# -*- coding: mbcs -*-
"""
HSK_63_E_3D_complete.py

Publication-ready Abaqus script for the generation of the full 3D HSK-E
reference geometry.

Purpose
-------
This script generates the complete 3D reference model used as an auxiliary
geometry definition in the Chapter 4 study environment.

Notes
-----
The script keeps the original hard-coded study settings and is cleaned up for
better readability and documentation only.
"""

from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *
import math
from math import pi, tan, sqrt


# ---------------------------------------------------------------------------
# Study control parameters
# ---------------------------------------------------------------------------
n = 1
reib = 0.15
Jobname = '3D_quad_Elements'
F_SP1 = -18000.0
drehzahl = 25000.0
F_Ax = -1000
M_tinNm = 53
F_Mb = 1000.0
g_Emod_H = 210000.0
g_rho_H = 0.0000000078

Mesh_Faktor = 2
A_mesh_size = Mesh_Faktor * 1.0
A_mesh_size_contactzone = Mesh_Faktor * 0.025
A_mesh_size_Uebergangsradius = Mesh_Faktor * 0.005
A_mesh_size_Kegelflaeche = Mesh_Faktor * 0.25
H_mesh_size = Mesh_Faktor * 1.0
H_mesh_size_ersatzfreistich = Mesh_Faktor * 0.025
H_mesh_size_r5 = Mesh_Faktor * 0.025
H_mesh_size_Kontaktflaechen = Mesh_Faktor * 0.025
H_mesh_size_Spannflaeche = Mesh_Faktor * 0.05
H_mesh_size_freistich = Mesh_Faktor * 0.05
H_mesh_size_Kontaktzone_freistich = Mesh_Faktor * 0.01
S_mesh_size = Mesh_Faktor * 1.0


# ---------------------------------------------------------------------------
# HSK geometry parameters
# ---------------------------------------------------------------------------
##HSK Variablen
H_d1=63.0
H_d2=48.01
H_d3=34.0
H_d4=40.0
H_d5=37.0
H_d6=8.4
H_d10=55.0
H_d11=72.3
H_d12=7.0
#H_d7=34.5 (O-Ringeinstich wird vernachlaessigt)
H_d15=18.0
H_L1=32.0
H_L2=6.3
H_L3=10.0
H_L4=6
H_L5=18.13
H_L6=10.0
H_L7=1.0
H_L9=12.0
H_L15=2.0
H_L16=1.0
H_e1=17.99
H_e2=20.87
H_f1=26.0
H_f3=18.0
H_f4=3.75
H_b1=12.54
H_b5=12.425
H_r1=1.2
H_r2=1.5
H_r3=2.88
H_r4=8.0
H_r5=0.6
H_r6=1.5
H_AL2=2.8681
#Freistich
H_rf=0.6
H_t1=0.25
H_t2=0.125
H_ff=2
#AL4=15 Grad Winkel Freistich
H_AL4=15.0
#AL5=8 Grad Winkel Freistich
H_AL5=8.0
##Werte ohne Norm 
#AL8 Winkel 27 Grad der Mitnehmernut
H_AL8=27.0
#r9 Nutgrundradius greiferrille
H_r9=0.75
#r10 Ersatzfreistich  
H_r10=0.3
#L20 Bundhoehe
H_L20=H_f1
#AL1 Spannwinkel
H_AL1=30.0
#AL3 Fasenwinkel bei L7 
H_AL3=30.0
#Oeffnungswinkel Greifernut
H_AL6=60.0
H_AL7=90.0-(H_AL6)
#Fasenwinkel Mitnehmernut
H_AL9=30.0
##Arbeitvariablen Null setzen
H_r11=0.0
H_r12=0.0
H_r13=0.0
H_r14=0.0
H_r15=0.0
H_r16=0.0
H_r17=0.0
H_r18=0.0
H_r19=0.0
H_r20=0.0
H_r21=0.0
H_r22=0.0
H_r23=0.0
H_r24=0.0
H_r25=0.0
H_r26=0.0
H_r27=0.0
H_r28=0.0
H_r29=0.0
H_r30=0.0
##Anfang Zeichnen
mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)
mdb.models['Model-1'].sketches['__profile__'].ConstructionLine(point1=(0.0, -100.0), point2=(0.0, 100.0))
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].geometry[2])
#Beginn Geometrie
#Mathematik
H_r11=(H_AL3*pi/180)
H_r12=(H_AL1*pi/180)
H_r13=(H_AL2*pi/180)
H_r25=(H_AL4*pi/180)
H_r26=(H_AL5*pi/180)
H_r14=(tan(H_r13)*(H_L2))
H_r15=(tan(H_r12)*(H_d5-H_d3)/2)
H_r16=(H_L1-H_L5-H_r15)
H_r17=(tan(H_r12)*(H_d4-H_d5)/2)
H_r18=(H_L1-H_L5+H_r17)
H_r19=(H_d2/2+H_r14)
H_r20=(tan(H_r13)*(H_L1-H_L2))
H_r21=(H_d2/2-H_r20)
H_r22=(tan(H_r11)*(H_L7))
H_r23=(H_d3/2+H_r22)
H_r24=(H_d5/2)
H_r27=(tan(H_r13)*(H_L2-H_ff))
H_r28=(H_r27+H_d2/2)
H_r29=(H_AL9*pi/180)*H_L16
H_r30=(H_AL9*pi/180)*H_L15
H=mdb.models['Model-1'].sketches['__profile__']

#Linie Sektion 3
H.Line(point1=(H_d3/2 , H_L7),point2=(H_d3/2 , H_r16))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[3])
H.FixedConstraint(entity= H.vertices[0])
H.FixedConstraint(entity= H.vertices[1])
#Linie Sektion 4 
H.Line(point1=(H_d3/2 , H_r16), point2=(H_d5/2 , (H_L1-H_L5)))
H.FixedConstraint(entity= H.vertices[2])
#Linie Sektion 5 
H.Line(point1=(H_d5/2 , H_L1-H_L5), point2=(H_d4/2 , H_r18))
H.FixedConstraint(entity= H.vertices[3])
#Linie Sektion 5
H.Line(point1=(H_d4/2, H_r18), point2=(H_d4/2 , 20))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[6])
#Kreisbogen Sektion 6
H.ArcByStartEndTangent(entity= H.geometry[6], point1=(H_d4/2, 20), point2=(H_d3/2, H_L1))
H.RadialDimension(curve= H.geometry[7], radius=(H_r4), textPoint=(11.875, 17.5))
H.DistanceDimension(entity1= H.vertices[5], entity2= H.geometry[2], textPoint=(6.2999267578125, 21.4470520019531), value=(H_d3/2))
H.VerticalDimension(textPoint=( 4.65307235717773, 8.4326114654541), value=(H_L1-H_L7), vertex1= H.vertices[5], vertex2= H.vertices[0])
H.FixedConstraint(entity= H.vertices[4])
H.FixedConstraint(entity= H.vertices[5])
H.FixedConstraint(entity= H.vertices[6])
#Linie Sektion 7
H.Line(point1=(H_d3/2, H_L1), point2=(H_d3/2 , (H_L1+H_L6)))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[8])
H.FixedConstraint(entity= H.vertices[7])
#Linie Sektion 8
H.Line(point1=(H_d3/2, (H_L1+H_L6)), point2=(H_d15/2 , (H_L1+H_L6)))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[9])
H.FixedConstraint(entity= H.vertices[8])
#Linie Sektion 9
H.Line(point1=(H_d15/2, (H_L1+H_L6) ), point2=(H_d15/2 , (H_L1+H_L6+H_L9)))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[10])
H.FixedConstraint(entity= H.vertices[9])
#Linie Sektion 10
H.Line(point1=(H_d15/2, (H_L1+H_L6+H_L9)), point2=(H_d6/2 , (H_L1+H_L6+H_L9)))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[11])
H.FixedConstraint(entity= H.vertices[10])
#Linie Sektion 11
H.Line(point1=(H_d6/2, (H_L1+H_L6+H_L9) ), point2=(H_d6/2 , (H_L1+H_L20)))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[12])
H.FixedConstraint(entity= H.vertices[11])
#Linie Sektion 12
H.Line(point1=(H_d6/2, (H_L1+H_L20)), point2=(H_d1/2 , (H_L1+H_L20)))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[13])
H.FixedConstraint(entity= H.vertices[12])
#Linie Sektion 13
H.Line(point1=(H_d1/2, (H_L1+H_L20) ), point2=(H_d1/2 , (H_L1)))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[14])
H.FixedConstraint(entity= H.vertices[13])
#Linie Sektion 14
H.Line(point1=(H_d1/2, (H_L1)), point2=(H_r19 , (H_L1)))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[15])
H.FixedConstraint(entity= H.vertices[14])
#Linie Sektion 15 
H.Line(point1=(H_r19 , H_L1), point2=(H_r21 , 0))
H.FixedConstraint(entity= H.vertices[15])
#Linie Sektion 16
H.Line(point1=(H_r21, 0), point2=( H_r23, 0))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[17])
H.FixedConstraint(entity= H.vertices[16])
#Linie Sektion 17 
H.Line(point1=(H_r23, 0), point2=(H_d3/2 , H_L7))
#Verrundung r5
H.FilletByRadius(curve1= H.geometry[5], curve2= H.geometry[6], nearPoint1=((H_d4/2-0.5), (H_r18-0.5)), nearPoint2=((H_d4/2+0.2), (H_r18+2)), radius= H_r5)
H.FixedConstraint(entity= H.vertices[17])
H.FixedConstraint(entity= H.vertices[18])
H.FixedConstraint(entity= H.vertices[19])
#Verrundung r6
H.FilletByRadius(curve1= H.geometry[8], curve2= H.geometry[9], nearPoint1=((H_d3/2+0.2), (H_L1+H_L6-3)), nearPoint2=((H_d3/2-2), (H_L1+H_L6+0.5)), radius= H_r6)
H.FixedConstraint(entity= H.vertices[22])
H.FixedConstraint(entity= H.vertices[23])
H.FixedConstraint(entity= H.vertices[24])
#Verrundung r10 (Ersatzfreistich)
H.FilletByRadius(curve1= H.geometry[15], curve2= H.geometry[16], nearPoint1=((H_r19+2), (H_L1+0.2)), nearPoint2=((H_r19-0.5), (H_L1-1)), radius= H_r10)
H.FixedConstraint(entity= H.vertices[27])
H.FixedConstraint(entity= H.vertices[28])
H.FixedConstraint(entity= H.vertices[29])
#Verrundung r1 
H.FilletByRadius(curve1= H.geometry[16], curve2= H.geometry[17], nearPoint1=((H_r21+2), (2)), nearPoint2=((H_r21-2), (0-0.1)), radius= H_r1)
H.FixedConstraint(entity= H.vertices[32])
H.FixedConstraint(entity= H.vertices[33])
H.FixedConstraint(entity= H.vertices[34])

#Loeschen Bemassungen
H.delete(objectList=(H.dimensions[0], ))
H.delete(objectList=(H.dimensions[1], ))
H.delete(objectList=(H.dimensions[2], ))
H.delete(objectList=(H.dimensions[3], ))
H.delete(objectList=(H.dimensions[4], ))
H.delete(objectList=(H.dimensions[5], ))
H.delete(objectList=(H.dimensions[6], ))

#Freistich nach Norm einfuegen (0 = Ersatzradius, 1=Freistich nach Norm)
q=0
if q == 0:
    #Greifernut einfuegen
    H.Line(point1=(H_d1/2, H_L1+H_f3-5), point2=(28.75, H_L1+H_f3-H_f4/2))
    H.CoincidentConstraint(entity1=H.vertices[37], entity2=H.geometry[14], addUndoState=False)
    H.Line(point1=(28.75, H_L1+H_f3-H_f4/2), point2=(H_d10/2, H_L1+H_f3-H_f4/2))
    H.HorizontalConstraint(entity=H.geometry[24], addUndoState=False)
    H.Line(point1=(H_d10/2, H_L1+H_f3-H_f4/2), point2=(H_d10/2, H_L1+H_f3+H_f4/2))
    H.VerticalConstraint(entity=H.geometry[25], addUndoState=False)
    H.PerpendicularConstraint(entity1=H.geometry[24], entity2=H.geometry[25], addUndoState=False)
    H.Line(point1=(H_d10/2, H_L1+H_f3+H_f4/2), point2=(28.75, H_L1+H_f3+H_f4/2))
    H.HorizontalConstraint(entity=H.geometry[26], addUndoState=False)
    H.PerpendicularConstraint(entity1=H.geometry[25], entity2=H.geometry[26], addUndoState=False)
    H.Line(point1=(28.75, H_L1+H_f3+H_f4/2), point2=(H_d1/2, H_L1+H_f3+5))
    H.CoincidentConstraint(entity1=H.vertices[42], entity2=H.geometry[14], addUndoState=False)
    H.FixedConstraint(entity=H.vertices[39])
    H.FixedConstraint(entity=H.vertices[40])
    H.ConstructionLine(point1=(28.75, H_L1+H_f3-H_f4/2), point2=(28.75, H_L1+H_f3+H_f4/2))
    H.CoincidentConstraint(entity1=H.vertices[38], entity2=H.geometry[28], addUndoState=False)
    H.CoincidentConstraint(entity1=H.vertices[41], entity2=H.geometry[28], addUndoState=False)
    H.VerticalConstraint(entity=H.geometry[28])
    H.AngularDimension(line1=H.geometry[27], line2=H.geometry[23], textPoint=(39.5776824951172, 44.4851303100586), value=H_AL6)
    H.AngularDimension(line1=H.geometry[27], line2=H.geometry[13], textPoint=(27.5408706665039,53.2741775512695), value=H_AL7)
    H.CircleByCenterPerimeter(center=(36.25, 46.25), point1=(33.75, 47.5))
    H.TangentConstraint(entity1=H.geometry[29], entity2=H.geometry[27])
    H.TangentConstraint(entity1=H.geometry[23], entity2=H.geometry[29])
    H.RadialDimension(curve=H.geometry[29], textPoint=(37.9049339294434, 45.8928146362305), radius=H_d12/2)
    H.ConstructionLine(point1=(43.75, 56.25), point2=(43.75, 40.0))
    H.VerticalConstraint(entity=H.geometry[30], addUndoState=False)
    H.TangentConstraint(entity1=H.geometry[30], entity2=H.geometry[29])
    H.DistanceDimension(entity1=H.geometry[30], entity2=H.geometry[2], textPoint=(15.0111827850342, 59.4167098999023), value=H_d11/2)
    H.setAsConstruction(objectList=(H.geometry[29], ))
    H.FixedConstraint(entity=H.vertices[37])
    H.FixedConstraint(entity=H.vertices[42])
    H.autoTrimCurve(curve1=H.geometry[14], point1=(31.3305892944336, 46.7319564819336))
    H.CoincidentConstraint(entity1=H.vertices[46], entity2=H.vertices[37])  
    H.CoincidentConstraint(entity1=H.vertices[45], entity2=H.vertices[42])
    H.FixedConstraint(entity=H.vertices[38])
    H.FixedConstraint(entity=H.vertices[41])
    H.FilletByRadius(radius=H_r9, curve1=H.geometry[24], nearPoint1=(H_d10/2+0.1, H_L1+H_f3-H_f4/2), curve2=H.geometry[25], nearPoint2=(H_d10/2, H_L1+H_f3-H_f4/2+0.1))
    H.FilletByRadius(radius=H_r9, curve1=H.geometry[25], nearPoint1=(H_d10/2, H_L1+H_f3+H_f4/2-0.1), curve2=H.geometry[26], nearPoint2=(H_d10/2+0.1, H_L1+H_f3+H_f4/2))
    
    mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Halter_Parameter', type=DEFORMABLE_BODY)
    mdb.models['Model-1'].parts['Halter_Parameter'].BaseSolidRevolve(angle=360.0,flipRevolveDirection=OFF, sketch= mdb.models['Model-1'].sketches['__profile__'])
    del mdb.models['Model-1'].sketches['__profile__']
    
    session.viewports['Viewport: 1'].setValues(displayedObject=mdb.models['Model-1'].parts['Halter_Parameter'])
           
    print('Radius statt Freistich')
else:
    H.delete(objectList=( H.geometry[21], ))
    H.ConstructionLine(point1=( H_d4/2, (H_L1+H_t2)), point2=(H_d1/2,(H_L1+H_t2)))
    H.HorizontalConstraint(addUndoState=False, entity=H.geometry[23])
    H.DistanceDimension(entity1=H.geometry[15], entity2=H.geometry[23], textPoint=(27.1692657470703, 32.4754295349121), value=H_t2)
    H.DistanceDimension(entity1= H.vertices[30], entity2=  H.geometry[2], textPoint=( 6.78884696960449, 35.9036750793457), value=H_r19+2)
    H.DistanceDimension(entity1=H.geometry[15], entity2=H.vertices[31], textPoint=(29.1119537353516, 31.4285888671875), value=H_ff)
    #Linie unter 8 Grad
    H.Line(point1=(H_r19+2, H_L1), point2=(H_r19-0.5, H_L1+H_t2-0.01))
    #Linie Parallel zur Drehachse im Freistich
    H.Line(point1=(H_r19-0.5, H_L1+H_t2-0.01), point2=(H_r19-0.5, H_L1-1.5))
    H.VerticalConstraint(addUndoState= False, entity=H.geometry[25])
    #Linie unter 15 Grad
    H.Line(point1=(H_r19-0.5, H_L1-1.5), point2=(H_r28, H_L1-H_ff))
    #Verrundungen im Freistich
    H.FilletByRadius(curve1=H.geometry[25], curve2=H.geometry[24], nearPoint1=( H_r19-0.5, H_L1+H_t2-0.3), nearPoint2=(H_r19-0.3, H_L1+H_t2-0.1), radius=H_rf)
    H.FilletByRadius(curve1= H.geometry[25], curve2= H.geometry[26], nearPoint1=( H_r19-0.5, H_L1-1.25), nearPoint2=(H_r19-0.3, H_L1-1.6), radius=H_rf)
    #Bedingungen einbauen
    H.DistanceDimension(entity1= H.vertices[31], entity2= H.geometry[25], textPoint=(24.0665969848633, 30.8173751831055), value=H_t1)
    H.delete(objectList=(H.dimensions[8], ))
    H.TangentConstraint(entity1=H.geometry[27], entity2= H.geometry[23])
    H.AngularDimension(line1=H.geometry[26], line2=H.geometry[16], textPoint=(24.3862228393555, 29.4134311676025), value=H_AL4)
    H.AngularDimension(line1= H.geometry[24], line2= H.geometry[15], textPoint=(27.9360599517822, 31.8545627593994), value=180-H_AL5)
    
    #Greifernut einfuegen
    H.Line(point1=(H_d1/2, H_L1+H_f3-5), point2=(28.75, H_L1+H_f3-H_f4/2))
    H.CoincidentConstraint(entity1=H.vertices[49], entity2=H.geometry[14], addUndoState=False)
    H.Line(point1=(28.75, H_L1+H_f3-H_f4/2), point2=(H_d10/2, H_L1+H_f3-H_f4/2))
    H.HorizontalConstraint(entity=H.geometry[30], addUndoState=False)
    H.Line(point1=(H_d10/2, H_L1+H_f3-H_f4/2), point2=(H_d10/2, H_L1+H_f3+H_f4/2))
    H.VerticalConstraint(entity=H.geometry[31], addUndoState=False)
    H.PerpendicularConstraint(entity1=H.geometry[30], entity2=H.geometry[31], addUndoState=False)
    H.Line(point1=(H_d10/2, H_L1+H_f3+H_f4/2), point2=(28.75, H_L1+H_f3+H_f4/2))
    H.HorizontalConstraint(entity=H.geometry[32], addUndoState=False)
    H.PerpendicularConstraint(entity1=H.geometry[31], entity2=H.geometry[32], addUndoState=False)
    H.Line(point1=(28.75, H_L1+H_f3+H_f4/2), point2=(H_d1/2, H_L1+H_f3+5))
    H.CoincidentConstraint(entity1=H.vertices[54], entity2=H.geometry[14], addUndoState=False)
    H.FixedConstraint(entity=H.vertices[51])
    H.FixedConstraint(entity=H.vertices[52])
    H.ConstructionLine(point1=(28.75, H_L1+H_f3-H_f4/2), point2=(28.75, H_L1+H_f3+H_f4/2))
    H.CoincidentConstraint(entity1=H.vertices[50], entity2=H.geometry[34], addUndoState=False)
    H.CoincidentConstraint(entity1=H.vertices[53], entity2=H.geometry[34], addUndoState=False)
    H.VerticalConstraint(entity=H.geometry[34])
    H.AngularDimension(line1=H.geometry[33], line2=H.geometry[29], textPoint=(39.5776824951172, 44.4851303100586), value=H_AL6)
    H.AngularDimension(line1=H.geometry[33], line2=H.geometry[13], textPoint=(27.5408706665039,53.2741775512695), value=H_AL7)
    H.CircleByCenterPerimeter(center=(36.25, 46.25), point1=(33.75, 47.5))
    H.TangentConstraint(entity1=H.geometry[35], entity2=H.geometry[33])
    H.TangentConstraint(entity1=H.geometry[29], entity2=H.geometry[35])
    H.RadialDimension(curve=H.geometry[35], textPoint=(37.9049339294434, 45.8928146362305), radius=H_d12/2)
    H.ConstructionLine(point1=(43.75, 56.25), point2=(43.75, 40.0))
    H.VerticalConstraint(entity=H.geometry[36], addUndoState=False)
    H.TangentConstraint(entity1=H.geometry[36], entity2=H.geometry[35])
    H.DistanceDimension(entity1=H.geometry[36], entity2=H.geometry[2], textPoint=(15.0111827850342, 59.4167098999023), value=H_d11/2)
    H.setAsConstruction(objectList=(H.geometry[35], ))
    H.FixedConstraint(entity=H.vertices[49])
    H.FixedConstraint(entity=H.vertices[54])
    H.autoTrimCurve(curve1=H.geometry[14], point1=(31.3305892944336, 46.7319564819336))
    H.CoincidentConstraint(entity1=H.vertices[58], entity2=H.vertices[49])  
    H.CoincidentConstraint(entity1=H.vertices[57], entity2=H.vertices[54])
    H.FixedConstraint(entity=H.vertices[50])
    H.FixedConstraint(entity=H.vertices[53])
    H.FilletByRadius(radius=H_r9, curve1=H.geometry[30], nearPoint1=(H_d10/2+0.1, H_L1+H_f3-H_f4/2), curve2=H.geometry[31], nearPoint2=(H_d10/2, H_L1+H_f3-H_f4/2+0.1))
    H.FilletByRadius(radius=H_r9, curve1=H.geometry[31], nearPoint1=(H_d10/2, H_L1+H_f3+H_f4/2-0.1), curve2=H.geometry[32], nearPoint2=(H_d10/2+0.1, H_L1+H_f3+H_f4/2))
    
    mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Halter_Parameter', type=DEFORMABLE_BODY)
    mdb.models['Model-1'].parts['Halter_Parameter'].BaseSolidRevolve(angle=180.0,flipRevolveDirection=OFF, sketch= mdb.models['Model-1'].sketches['__profile__'])
    del mdb.models['Model-1'].sketches['__profile__']

    print('Kontur mit Freistich')

### Dummyspannbereich einfuegen
p = mdb.models['Model-1'].parts['Halter_Parameter']
s = p.features['Solid revolve-1'].sketch
mdb.models['Model-1'].ConstrainedSketch(name='__edit__', objectToCopy=s)
s2 = mdb.models['Model-1'].sketches['__edit__']
g, v, d, c = s2.geometry, s2.vertices, s2.dimensions, s2.constraints
s2.setPrimaryObject(option=SUPERIMPOSE)
p.projectReferencesOntoSketch(sketch=s2, 
    upToFeature=p.features['Solid revolve-1'], filter=COPLANAR_EDGES)
s2.Line(point1=(7.5, 58.0), point2=(7.5, 165.0))
s2.VerticalConstraint(entity=g[35], addUndoState=False)
s2.PerpendicularConstraint(entity1=g[13], entity2=g[35], addUndoState=False)
s2.CoincidentConstraint(entity1=v[57], entity2=g[13], addUndoState=False)
s2.Line(point1=(7.5, 165.0), point2=(26.5, 165.0))
s2.HorizontalConstraint(entity=g[36], addUndoState=False)
s2.PerpendicularConstraint(entity1=g[35], entity2=g[36], addUndoState=False)
s2.Line(point1=(26.5, 165.0), point2=(26.5, 58.0))
s2.VerticalConstraint(entity=g[37], addUndoState=False)
s2.PerpendicularConstraint(entity1=g[36], entity2=g[37], addUndoState=False)
s2.autoTrimCurve(curve1=g[13], point1=(21.0845546722412, 58.3236999511719))
s2.delete(objectList=(d[10], ))
s2.delete(objectList=(d[7], ))
s2.unsetPrimaryObject()
p = mdb.models['Model-1'].parts['Halter_Parameter']
p.features['Solid revolve-1'].setValues(sketch=s2)
del mdb.models['Model-1'].sketches['__edit__']
p = mdb.models['Model-1'].parts['Halter_Parameter']
p.regenerate()
print('Erstellung Halter erfolgreich')

##HSK Variablen
A_b1=12.3
A_d1=63.0
#A_d2=47.998
A_d2=47.995 #Werte fuer HSK-E
A_d3=34.0
A_d4=46.53
A_d5=47.2
A_L1=33.0
A_L2=6.3
#A_L3=22.4
A_L3=32.0 #Werte fuer HSK-E
A_L4=26.4
A_L5=1.0
A_L6=1.5
A_L7=2.5
A_r1=3.0
A_AL2=2.8624
##Werte ohne Norm 
#s_l_esp  = float(getInput('Einspannlaenge:'))
s_l_esp  = 100.0

print('Einspannlaenge = %s mm'% (s_l_esp))
#L20 Laenge bis zur Spindel
A_L20=s_l_esp
#AL3 Fasenwinkel bei L5 
A_AL3=30.0
#d10 Hohlzylinder Durchmesser Spindel = D3
A_d10=A_d3
##Arbeitvariablen Null setzen
A_r11=0.0
A_r13=0.0
A_r14=0.0
A_r15=0.0
A_r16=0.0
A_r17=0.0
A_r18=0.0
A_r19=0.0
##Anfang Zeichnen
mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)
mdb.models['Model-1'].sketches['__profile__'].ConstructionLine(point1=(0.0, -100.0), point2=(0.0, 100.0))
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].geometry[2])
#Beginn Geometrie
#Mathematik
A_r11=(A_AL3*pi/180)
A_r13=(A_AL2*pi/180)
A_r14=(tan(A_r13)*(A_L2-A_L5))
A_r15=(tan(A_r11)*(A_L5))
A_r16=(A_d2/2+A_r14)
A_r17=(tan(A_r13)*(A_L3-A_L7-A_L2))
A_r18=(A_r16+A_r15)
A_r19=(A_d2/2-A_r17)
#Linie Sektion 3
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_r18 , 0),point2=(A_d1/2 , 0))
mdb.models['Model-1'].sketches['__profile__'].HorizontalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[3])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[0])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[1])
#Linie Sektion 4 
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_d1/2 , 0), point2=(A_d1/2 , (-1*A_L20)))
mdb.models['Model-1'].sketches['__profile__'].VerticalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[4])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[2])
#Linie Sektion 5
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_d1/2, (-1*A_L20)), point2=(A_d10/2 , (-1*A_L20)))
mdb.models['Model-1'].sketches['__profile__'].HorizontalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[5])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[3])
#Linie Sektion 6
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_d10/2, (-1*A_L20)), point2=(A_d10/2 , (-1*A_L1)))
mdb.models['Model-1'].sketches['__profile__'].VerticalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[6])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[4])
#Linie Sektion 7
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_d10/2, (-1*A_L1)), point2=(A_d4/2 , (-1*A_L1)))
mdb.models['Model-1'].sketches['__profile__'].HorizontalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[7])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[5])
#Linie Sektion 8
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_d4/2, (-1*A_L1) ), point2=(A_d4/2 , (-1*A_L3)))
mdb.models['Model-1'].sketches['__profile__'].VerticalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[8])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[6])
#Linie Sektion 9
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_d4/2, (-1*A_L3)), point2=(A_d5/2 , (-1*A_L3)))
mdb.models['Model-1'].sketches['__profile__'].HorizontalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[9])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[7])
#Linie Sektion 10
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_d5/2, (-1*A_L3) ), point2=(A_d5/2 , (A_L7-A_L3)))
mdb.models['Model-1'].sketches['__profile__'].VerticalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[10])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[8])
#Linie Sektion 11
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_d5/2, (A_L7-A_L3)), point2=(A_r19 , (A_L7-A_L3)))
mdb.models['Model-1'].sketches['__profile__'].HorizontalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[11])
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[9])
#Linie Sektion 12
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_r19, (A_L7-A_L3) ), point2=(A_r16 , (-1*A_L5)))
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].vertices[10])
#Linie Sektion 13
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(A_r16, (-1*A_L5)), point2=(A_r18 , 0))
#Part erzeugen
mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Aufnahme_Parameter', type=DEFORMABLE_BODY)
mdb.models['Model-1'].parts['Aufnahme_Parameter'].BaseSolidRevolve(angle=360.0, flipRevolveDirection=OFF,sketch= mdb.models['Model-1'].sketches['__profile__'])
del mdb.models['Model-1'].sketches['__profile__']

###Spannband
## Spannband Variablen
S_pp=0.000
S_d1=A_d1-S_pp
S_ws=8.0
S_d2=S_d1+S_ws
S_L1=A_L1
##Anfang Zeichnen
mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)
mdb.models['Model-1'].sketches['__profile__'].sketchOptions.setValues(viewStyle=AXISYM)
mdb.models['Model-1'].sketches['__profile__'].ConstructionLine(point1=(0.0, -100.0), point2=(0.0, 100.0))
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].geometry[2])
S=mdb.models['Model-1'].sketches['__profile__']
#Linie Sektion 3
S.Line(point1=(S_d1/2 , 0),point2=(S_d2/2 , 0))
S.HorizontalConstraint(addUndoState=False, entity=S.geometry[3])
S.FixedConstraint(entity= S.vertices[0])
S.FixedConstraint(entity= S.vertices[1])
#Linie Sektion 4 
S.Line(point1=(S_d2/2 , 0), point2=(S_d2/2 , (-1*S_L1)))
S.VerticalConstraint(addUndoState=False, entity=S.geometry[4])
S.FixedConstraint(entity= S.vertices[2])
#Linie Sektion 5
S.Line(point1=(S_d2/2, (-1*S_L1)), point2=(S_d1/2 , (-1*S_L1)))
S.HorizontalConstraint(addUndoState=False, entity=S.geometry[5])
S.FixedConstraint(entity= S.vertices[3])
#Linie Sektion 6
S.Line(point1=(S_d1/2, (-1*S_L1)), point2=(S_d1/2 , 0))
S.VerticalConstraint(addUndoState=False, entity=S.geometry[6])
#Part erzeugen
mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Spannband', type=DEFORMABLE_BODY)
mdb.models['Model-1'].parts['Spannband'].BaseSolidRevolve(angle=360.0, flipRevolveDirection=OFF,sketch= mdb.models['Model-1'].sketches['__profile__'])
del mdb.models['Model-1'].sketches['__profile__']
print('Erstellung Spannband erfolgreich')

###Meshing
import mesh
##Elementtypen
elemType1 = mesh.ElemType(elemCode=C3D8R, elemLibrary=STANDARD, kinematicSplit=AVERAGE_STRAIN, secondOrderAccuracy=OFF, hourglassControl=DEFAULT, distortionControl=DEFAULT)
elemType2 = mesh.ElemType(elemCode=C3D6, elemLibrary=STANDARD)
elemType3 = mesh.ElemType(elemCode=C3D4, elemLibrary=STANDARD)
## Meshing Aufnahme
p = mdb.models['Model-1'].parts['Aufnahme_Parameter']
c = p.cells
pickedRegions = c[0:1]
p.setMeshControls(regions=pickedRegions, elemShape=HEX_DOMINATED)
cells = c[0:1]
pickedRegions =(cells, )
p.setElementType(regions=pickedRegions, elemTypes=(elemType1, elemType2, elemType3))
p.seedPart(size=A_mesh_size, deviationFactor=0.1, minSizeFactor=0.1)
e = p.edges
pickedEdges = e[19:20]
p.seedEdgeBySize(edges=pickedEdges, size=A_mesh_size_Kegelflaeche, deviationFactor=0.1, constraint=FINER)
#p = mdb.models['Model-1'].parts['Aufnahme_Parameter']
p.generateMesh()
print('Aufnahme Mesh erfolgreich')

## Meshing Halter
p = mdb.models['Model-1'].parts['Halter_Parameter']
c = p.cells
pickedRegions = c[0:1]
p.setMeshControls(regions=pickedRegions, elemShape=HEX_DOMINATED)
cells = c[0:1]
pickedRegions =(cells, )
p.setElementType(regions=pickedRegions, elemTypes=(elemType1, elemType2, elemType3))
p.seedPart(size=H_mesh_size, deviationFactor=0.1, minSizeFactor=0.1)
# if q == 0:
    # p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#10 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_ersatzfreistich)
    # p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#10000 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_r5)
    # p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#10 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_Kontaktzone_fase)
    # print('Ersatzradius')
# else:
    # p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#e0 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_freistich)
    # p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#100000 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_r5)
    # p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#110 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_Kontaktzone_freistich)
    # print('Mit Freistich')

# p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.findAt(((H_d2/2, (H_L1-H_L2), 0.0), ), ((H_d1/2-0.1, H_L1, 0.0), ), ), minSizeFactor=0.1, size= H_mesh_size_Kontaktflaechen)
# p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.findAt((((H_d5/2+1), (H_L1+1*tan(H_r12)-H_L5), 0.0), ), (((H_d5/2-1), (H_L1-1*tan(H_r12)-H_L5), 0.0), ), ), minSizeFactor=0.1, size=H_mesh_size_Spannflaeche)
#p = mdb.models['Model-1'].parts['Halter_Parameter']
p.generateMesh()
print('WZH Mesh erfolgreich')

## Meshing Spannband
p1 = mdb.models['Model-1'].parts['Spannband']
elemType1 = mesh.ElemType(elemCode=C3D8R, elemLibrary=STANDARD, 
    kinematicSplit=AVERAGE_STRAIN, secondOrderAccuracy=OFF, 
    hourglassControl=DEFAULT, distortionControl=DEFAULT)
elemType2 = mesh.ElemType(elemCode=C3D6, elemLibrary=STANDARD)
elemType3 = mesh.ElemType(elemCode=C3D4, elemLibrary=STANDARD)
p = mdb.models['Model-1'].parts['Spannband']
c = p.cells
cells = c[0:1]
pickedRegions =(cells, )
p.setElementType(regions=pickedRegions, elemTypes=(elemType1, elemType2, 
    elemType3))
p.seedPart(size=S_mesh_size, deviationFactor=0.1, minSizeFactor=0.1)
p.generateMesh()
print('Spannband Mesh erfolgreich')

print('Mesh komplett')

### Create Materials:
Emod_H = g_Emod_H
rho_H = g_rho_H
m = mdb.models['Model-1']
m.Material(name='Halter')
m.materials['Halter'].Density(table=((rho_H, ), ))
m.materials['Halter'].Elastic(table=((Emod_H, 0.3), ))
print('Der E-Modul des Halter betraegt %E N/mm^2'% (Emod_H))

#Stahl
m = mdb.models['Model-1']
m.Material(name='Stahl')
m.materials['Stahl'].Density(table=((0.0000000078, ), ))
m.materials['Stahl'].Elastic(table=((210000.0, 0.3), ))

#Spannband
Emod_S=210000.0
m = mdb.models['Model-1']
m.Material(name='Spannband')
m.materials['Spannband'].Density(table=((0.0000000078, ), ))
m.materials['Spannband'].Elastic(table=((Emod_S, 0.3), ))
print('Der E-Modul des Spannbandes betraegt %E N/mm^2'% (Emod_S))

#Spindelaufnahme
Emod_A=210000.0
m = mdb.models['Model-1']
m.Material(name='Spindelaufnahme')
m.materials['Spindelaufnahme'].Density(table=((0.0000000078, ), ))
m.materials['Spindelaufnahme'].Elastic(table=((Emod_A, 0.3), ))
print('Der E-Modul der Spindelaufnahme betraegt %E N/mm^2'% (Emod_A))

# HexMC:
#mdb.models['Model-1'].Material(name='HexMC')
#m.materials['HexMC'].Density(table=((0.00000000155, ), ))
#mdb.models['Model-1'].materials['HexMC'].Elastic(type=ENGINEERING_CONSTANTS,table=((12039.0, 28727.0, 28727.0, 0.24, 0.24, 0.2779, 4849.0, 4849.0, 11240.0), ))

### Create Section:
mdb.models['Model-1'].HomogeneousSolidSection(material='Halter', name='Halter', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='Stahl', name='Stahl', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='Spannband', name='Spannband', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='Spindelaufnahme', name='Spindelaufnahme', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='HexMC', name='HexMC', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='CFK10', name='CFK10', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='CFK00', name='CFK00', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='CFK15', name='CFK15', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='CFK30', name='CFK30', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='CFK45', name='CFK45', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='CFK82', name='CFK82', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='CFK90', name='CFK90', thickness=None)

#### Section Assignement
##Section Assignment Spindel
p = mdb.models['Model-1'].parts['Aufnahme_Parameter']
c = p.cells
cells = c[0:1]
region = p.Set(cells=cells, name='Aufnahme_Parameter')
p = mdb.models['Model-1'].parts['Aufnahme_Parameter']
p.SectionAssignment(region=region, sectionName='Stahl', offset=0.0, 
    offsetType=MIDDLE_SURFACE, offsetField='', 
    thicknessAssignment=FROM_SECTION)
print('Section Assignment Aufnahme erfolgreich')	
mdb.models['Model-1'].rootAssembly.regenerate()
##Section assignment Werkzeugspannfutter
p = mdb.models['Model-1'].parts['Halter_Parameter']
c = p.cells
cells = c.findAt(((31.344513, 54.918332, -3.125945), ))
region = p.Set(cells=cells, name='Halter_Parameter')
p = mdb.models['Model-1'].parts['Halter_Parameter']
p.SectionAssignment(region=region, sectionName='Halter', offset=0.0, 
    offsetType=MIDDLE_SURFACE, offsetField='', 
    thicknessAssignment=FROM_SECTION)
print('Section Assignment Halter erfolgreich')	
##Section Assignment Spannband
p = mdb.models['Model-1'].parts['Spannband']
c = p.cells
cells = c.findAt(((S_d1/2 + 1.0, 0-1 , 0.0),(S_d1/2 + 2.0, 0-2 , 0.0),))
region = p.Set(cells=cells, name='Spannband')
p.SectionAssignment(region=region, sectionName='Spannband', offset=0.0, 
    offsetType=MIDDLE_SURFACE, offsetField='', 
    thicknessAssignment=FROM_SECTION)
print('Section Assignment Spannband erfolgreich')	
print('Sections erfolreich zugewiesen')

## Create Assembly
mdb.models['Model-1'].rootAssembly.regenerate()
m = mdb.models['Model-1']
a = m.rootAssembly
a.DatumCsysByDefault(CARTESIAN)
a.Instance(name='Aufnahme_Parameter', part=mdb.models['Model-1'].parts['Aufnahme_Parameter'], dependent=ON)
a.Instance(name='Halter_Parameter', part=mdb.models['Model-1'].parts['Halter_Parameter'], dependent=ON)
a.translate(instanceList=('Aufnahme_Parameter', ), vector=(0.0, 32.0, 0.0))
a.Instance(name='Spannband', part=mdb.models['Model-1'].parts['Spannband'], dependent=ON)
a.translate(instanceList=('Spannband', ), vector=(0.0, 32.0, 0.0))
print('Zusammenbau erfolgreich')

##Sets anlegen
p = mdb.models['Model-1'].parts['Halter_Parameter']
f = p.faces
faces = f[1:2]
p.Set(faces=faces, name='Spannflaeche')

s = p.faces
side1Faces = s[1:2]
p.Surface(side1Faces=side1Faces, name='Flaeche_Spannkraft')

### Lastfaelle
mdb.models['Model-1'].StaticStep(name='Uebermass', previous='Initial', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Stabilisierung', previous='Uebermass', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Vorspannkraft', previous='Stabilisierung', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Radialbeschleunigung', previous='Vorspannkraft', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Axialkraft', previous='Radialbeschleunigung', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Drehmoment', previous='Axialkraft', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Biegemoment', previous='Drehmoment', nlgeom=ON, initialInc = 0.1)

#Berechnung der Kraefte und Pressungen
##Spannkraft
F_SP2=(tan(H_AL1*pi/180)*F_SP1*-1)
F_SPges=math.sqrt(F_SP1*F_SP1+F_SP2*F_SP2)
#Flaeche des Spannkegels
#Mantelflaeche theoretischer Kegel- nicht vorhandene spitze
r_th=H_d4/2-0.3
h_th=tan(H_AL1*pi/180)*r_th
s_th=math.sqrt(r_th*r_th+h_th*h_th)
M_th=pi*r_th*s_th
r_th2=H_d3/2
h_th2=tan(H_AL1*pi/180)*r_th2
s_th2=math.sqrt(r_th2*r_th2+h_th2*h_th2)
M_th2=pi*r_th2*s_th2
M_r=M_th-M_th2
pr_SP=F_SPges/M_r
print('Die resultierende Kraft aus der Spannkraft betraegt = %F N'% (F_SPges))

#Spannkraft aufbringen
region = a.instances['Halter_Parameter'].surfaces['Flaeche_Spannkraft']
mdb.models['Model-1'].Pressure(name='Spannkraft_Flaechenpressung', 
    createStepName='Vorspannkraft', region=region, distributionType=UNIFORM, 
    field='', magnitude=pr_SP, amplitude=UNSET)

#Fliehkraft Drehzahl in U/min mit Einheitentuning
wurzelkorrektur=math.sqrt(1000.0)
kreisgeschwindigkeit = drehzahl/(60*2*pi)*wurzelkorrektur
J=mdb.models['Model-1']
J.RotationalBodyForce(centrifugal=ON, createStepName='Radialbeschleunigung', magnitude=kreisgeschwindigkeit, name='Zentrifugalkraft_Halter', point1=(0.0, 0.0, 0.0), point2=(0.0, 1.0, 0.0), region= J.rootAssembly.instances['Halter_Parameter'].sets['Halter_Parameter'], rotaryAcceleration=OFF)
J.RotationalBodyForce(centrifugal=ON, createStepName='Radialbeschleunigung', magnitude=kreisgeschwindigkeit, name='Zentrifugalkraft_Aufnahme', point1=(0.0, 0.0, 0.0), point2=(0.0, 1.0, 0.0), region= J.rootAssembly.instances['Aufnahme_Parameter'].sets['Aufnahme_Parameter'], rotaryAcceleration=OFF)
J.RotationalBodyForce(centrifugal=ON, createStepName='Radialbeschleunigung', magnitude=kreisgeschwindigkeit, name='Zentrifugalkraft_Spannband', point1=(0.0, 0.0, 0.0), point2=(0.0, 1.0, 0.0), region= J.rootAssembly.instances['Spannband'].sets['Spannband'], rotaryAcceleration=OFF)

##Randbedingunen+Kontaktflaechen
a = mdb.models['Model-1'].rootAssembly
f1 = a.instances['Aufnahme_Parameter'].faces
faces1 = f1[2:3]
a.Set(faces=faces1, name='Einspannung')
s1 = a.instances['Aufnahme_Parameter'].faces
side1Faces1 = s1[0:1]
a.Surface(side1Faces=side1Faces1, name='Plananlage Aufnahme')
#: The surface 'Kegel Aufnahme' has been edited (1 face).
s1 = a.instances['Aufnahme_Parameter'].faces
side1Faces1 = s1[9:10]
a.Surface(side1Faces=side1Faces1, name='Kegel Aufnahme')

if q == 0:
	s1 = a.instances['Halter_Parameter'].faces
	side1Faces1 = s1[27:28]
	a.Surface(side1Faces=side1Faces1, name='Kegel Halter')
	J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#10000 ]', ), ), name='Radius_Spannflaeche')
	J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#10 ]', ), ), name='Ersatzradius_am_Kegel')
else:
    J.rootAssembly.Surface(name='Kegel Halter', side1Edges=J.rootAssembly.instances['Halter_Parameter'].edges.findAt(((H_d2/2, (-1*H_L2+0.2), 0.0), )))
    J.rootAssembly.Surface(side1Edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#18 ]', ), ),name='Kegel Halter')
    J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#70 ]', ), ), name='Freistich-y-anteil')
    J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#180 ]', ), ), name='Freistich-x-anteil')
    J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#100000 ]', ), ), name='Radius_Spannflaeche')

s1 = a.instances['Halter_Parameter'].faces
side1Faces1 = s1[25:26]
a.Surface(side1Faces=side1Faces1, name='Plananlage Aufnahme')
f1 = a.instances['Halter_Parameter'].faces
faces1 = f1[25:26]
a.Set(faces=faces1, name='Plananlage_Halter')
s1 = a.instances['Halter_Parameter'].faces
side1Faces1 = s1[25:26]
a.Surface(side1Faces=side1Faces1, name='Plananlage Halter')
s1 = a.instances['Aufnahme_Parameter'].faces
side1Faces1 = s1[1:2]
a.Surface(side1Faces=side1Faces1, name='Spannband Aufnahme')
s1 = a.instances['Spannband'].faces
side1Faces1 = s1[3:4]
a.Surface(side1Faces=side1Faces1, name='Spannband Spannband')
J.rootAssembly.Set(edges= J.rootAssembly.instances['Aufnahme_Parameter'].edges.findAt(((A_d1/2, -8.25, 0.0), )), name='Spannband_Aufnahme')
f1 = a.instances['Aufnahme_Parameter'].faces
faces1 = f1[0:1]
a.Set(faces=faces1, name='Flaechenpressung-Plan')
f1 = a.instances['Aufnahme_Parameter'].faces
faces1 = f1[9:10]
a.Set(faces=faces1, name='Kegel')
f1 = a.instances['Halter_Parameter'].faces
faces1 = f1[27:28]
a.Set(faces=faces1, name='Kegel_Halter')
e1 = a.instances['Halter_Parameter'].edges
edges1 = e1[56:57]
a.Set(edges=edges1, name='Kegel_Halter_Linie')
a = mdb.models['Model-1'].rootAssembly
e1 = a.instances['Spannband'].edges
edges1 = e1[4:5]
a.Set(edges=edges1, name='Fixpunkt Spannband')
s1 = a.instances['Halter_Parameter'].faces
side1Faces1 = s1[13:14]
a.Surface(side1Faces=side1Faces1, name='Betriebskraftstelle')
e1 = a.instances['Halter_Parameter'].edges
edges1 = e1[29:30]
a.Set(edges=edges1, name='U2-Sperr')
f1 = a.instances['Halter_Parameter'].faces
faces1 = f1[25:26]
a.Set(faces=faces1, name='Planflaeche_Halter')
e1 = a.instances['Halter_Parameter'].edges
edges1 = e1[52:53]
a.Set(edges=edges1, name='Planflaeche_Halter_Linie')

#Betriebskraft
#Axialkraft negativ=zug positiv=druck
F_Ax=-1000
p_Ax=F_Ax/(pi/4*(H_d1*H_d1-(H_d6*H_d6)))
J.Pressure(name='Axialkraft', createStepName='Axialkraft', region=J.rootAssembly.surfaces['Betriebskraftstelle'], distributionType=UNIFORM, field='', magnitude=p_Ax, amplitude=UNSET)
J.loads['Axialkraft'].deactivate('Drehmoment')

#Drehmoment
#Referenzpunkt fuer Moment
J.rootAssembly.ReferencePoint(point=(0.0, 200.0, 0.0))
J.Coupling(name='Drehmoment', controlPoint=J.rootAssembly.Set(referencePoints=(J.rootAssembly.referencePoints[29], ), name='refPoint'), surface=J.rootAssembly.surfaces['Betriebskraftstelle'], influenceRadius=WHOLE_SURFACE, couplingType=KINEMATIC, localCsys=None, u1=ON, u2=ON, ur3=ON)
M_t=M_tinNm*1000
J.Moment(name='Drehmoment', createStepName='Drehmoment', region=J.rootAssembly.sets['refPoint'], cm2=M_t, distributionType=UNIFORM, field='', localCsys=None)

#Biegemoment
a = mdb.models['Model-1'].rootAssembly
r1 = a.referencePoints
refPoints1=(r1[29], )
region = a.Set(referencePoints=refPoints1, name='Set-15')
mdb.models['Model-1'].ConcentratedForce(name='Biegemoment', 
    createStepName='Biegemoment', region=region, cf1=F_Mb, 
    distributionType=UNIFORM, field='', localCsys=None)

#Einspannung
J.EncastreBC(createStepName='Initial', localCsys=None, name='Feste Einspannung', region=J.rootAssembly.sets['Einspannung'])
#Eckpunkt oben links U2 Richtung gesperrt nur wahrend Uebermass
#mdb.models['Model-1'].DisplacementBC(amplitude=UNSET, createStepName='Initial', distributionType=UNIFORM, fieldName='', localCsys=None, name='Fixpunkt', region=mdb.models['Model-1'].rootAssembly.sets['U2-Sperr'], u1=UNSET, u2=SET, ur3=UNSET)
#mdb.models['Model-1'].boundaryConditions['Fixpunkt'].deactivate('Vorspannkraft')
#Eckpunkt unten rechts in U2 Richtung sperren spannband
J.DisplacementBC(amplitude=UNSET, createStepName='Initial', distributionType=UNIFORM, fieldName='', localCsys=None, name='Fixpunkt Spannband', region=mdb.models['Model-1'].rootAssembly.sets['Fixpunkt Spannband'], u1=UNSET, u2=SET, ur3=UNSET)

#Verschiebung um -0.2 in U2 Richtung
J.DisplacementBC(name='Einzug Halter', createStepName='Uebermass', region=J.rootAssembly.sets['Planflaeche_Halter'], u1=UNSET, u2=-0.2, ur3=UNSET, amplitude=UNSET, fixed=OFF, distributionType=UNIFORM, fieldName='', localCsys=None)
J.boundaryConditions['Einzug Halter'].deactivate('Vorspannkraft')

#Aufklappen der Spannhuelse
#J.DisplacementBC(amplitude=UNSET, createStepName='Initial', distributionType=UNIFORM, fieldName='', localCsys=None, name='Aufklappen verhindern', region= J.rootAssembly.sets['Spannband'], u1=SET, u2=UNSET, ur3=UNSET)
#J.boundaryConditions['Aufklappen verhindern'].deactivate('Aufklappen')

J.ContactProperty('Normalkontakt')
J.interactionProperties['Normalkontakt'].NormalBehavior(allowSeparation=ON, constraintEnforcementMethod=DEFAULT, pressureOverclosure=HARD)
J.ContactProperty('Reibungsbehaftet')
J.interactionProperties['Reibungsbehaftet'].TangentialBehavior(dependencies=0, directionality=ISOTROPIC, elasticSlipStiffness=None, formulation=PENALTY, fraction=0.005, maximumElasticSlip=FRACTION, pressureDependency=OFF, shearStressLimit=None, slipRateDependency=OFF, table=((reib, ), ), temperatureDependency=OFF)
J.interactionProperties['Reibungsbehaftet'].NormalBehavior(allowSeparation=ON, constraintEnforcementMethod=DEFAULT, pressureOverclosure=HARD)

J.StdInitialization(name='Presspassung', overclosureType= INTERFERENCE,overclosureTolerance=0.2)
J.ContactStd(createStepName='Initial', name='Kontakt')
J.interactions['Kontakt'].includedPairs.setValuesInStep(stepName='Initial', useAllstar=ON)

if n == 0:
    J.interactions['Kontakt'].contactPropertyAssignments.appendInStep(assignments=((GLOBAL, SELF, 'Normalkontakt'), ), stepName='Initial')
    J.interactions['Kontakt'].initializationAssignments.appendInStep(stepName='Initial', assignments=((J.rootAssembly.surfaces['Spannband Aufnahme'], J.rootAssembly.surfaces['Spannband Spannband'], 'Presspassung'), ))
    print('Bedingungen ohne Reibung abgeschlossen')
else:
    J.interactions['Kontakt'].contactPropertyAssignments.appendInStep(assignments=((GLOBAL, SELF, 'Reibungsbehaftet'), ), stepName='Initial')
    J.interactions['Kontakt'].initializationAssignments.appendInStep(stepName='Initial', assignments=((J.rootAssembly.surfaces['Spannband Aufnahme'], J.rootAssembly.surfaces['Spannband Spannband'], 'Presspassung'), ))
    print('Bedingungen mit Reibung abgeschlossen')

mdb.models['Model-1'].FieldOutputRequest(name='Ausgang',  createStepName='Uebermass', variables=('S', 'E', 'U', 'CSTRESS', 'CDISP'))

mdb.Job(Jobname, model='Model-1', description='', type=ANALYSIS, 
    atTime=None, waitMinutes=0, waitHours=0, queue=None, memory=90, 
    memoryUnits=PERCENTAGE, getMemoryFromAnalysis=True, 
    explicitPrecision=SINGLE, nodalOutputPrecision=SINGLE, echoPrint=OFF, 
    modelPrint=OFF, contactPrint=OFF, historyPrint=OFF, userSubroutine='', 
    scratch='', resultsFormat=ODB, multiprocessingMode=DEFAULT, numCpus=2, 
    numDomains=2, numGPUs=0)

#mdb.jobs[Jobname].submit(consistencyChecking=OFF)
#mdb.jobs[Jobname].waitForCompletion()