Also, creating a robust script is different than the actions a user would normally use in the GUI.
You want to get the returned entities from creation functions. Most results have a list of a specific type underneath, such as .CreatedPlanes .CreatedCurves, .CreatedBodies, CreatedFaces, etc...
face1 = result.CreatedFaces[0]
You can use dir(result) to see the objects underneath.
Use .GetCreated to get entities of a type:
result = DatumPlaneCreator.Create(origin, Direction)
plane1 = result.GetCreated[IDatumPlane]()
Also, you want to use bottom-up type methodology so you don't lose a reference to the previous entities created. So that means: create points/curves, then faces, then bodies. When sketching , you'll want to turn on "Layout sketch" in the options.
This will record the command:
SketchHelper.LayoutCurves = True
So you'll just have curves when you go to 3D mode. You will need to create a surface with Fill or Blend, but you'll still have the curve references when you go to 3D and you'll then have the surface reference when you create the surface.
ClearAll() Â Â # good to do this at the beginning so you always start from the same state, and entity indices don't continue from some previously defined indices
extrudeDepth = MM(40)
BaseLength = MM(20)
BaseWidth = MM(16)
# Set Sketch Plane
sectionPlane = Plane.PlaneXY
result = ViewHelper.SetSketchPlane(sectionPlane, None)
# Set Layout Curves
SketchHelper.StartConstraintSketching()
SketchHelper.LayoutCurves = True
# Sketch Line
start = Point2D.Create(MM(0), MM(0))
end = Point2D.Create(BaseLength, MM(0))
result = SketchLine.Create(start, end)
crv1 = result.CreatedCurves[0]
# Sketch Line
start = Point2D.Create(BaseLength, MM(0))
end = Point2D.Create(BaseLength, BaseWidth)
result = SketchLine.Create(start, end)
crv2 = result.CreatedCurves[0]
# Sketch Line
start = Point2D.Create(BaseLength, BaseWidth)
end = Point2D.Create(MM(0), BaseWidth)
result = SketchLine.Create(start, end)
crv3 = result.CreatedCurves[0]
# Sketch Line
start = Point2D.Create(MM(0), BaseWidth)
end = Point2D.Create(MM(0), MM(0))
result = SketchLine.Create(start, end)
crv4 = result.CreatedCurves[0]
# Solidify Sketch
mode = InteractionMode.Solid
result = ViewHelper.SetViewMode(mode, None)
# Extrude 1 Edge
selection = Selection.Create(crv1)
options = ExtrudeEdgeOptions()
pnt1 = crv1.Shape.StartPoint
result = ExtrudeEdges.Execute(selection, pnt1, Direction.DirZ, extrudeDepth, options)
face1 = result.CreatedFaces[0]
body1 = face1.Parent
# Extrude 1 Edge
selection = Selection.Create(crv2)
pnt1 = crv2.Shape.StartPoint
result = ExtrudeEdges.Execute(selection, pnt1, Direction.DirZ, extrudeDepth, options)
face2 = result.CreatedFaces[0]
body2 = face2.Parent
# Extrude 1 Edge
selection = Selection.Create(crv3)
pnt1 = crv3.Shape.StartPoint
result = ExtrudeEdges.Execute(selection, pnt1, Direction.DirZ, extrudeDepth, options)
face3 = result.CreatedFaces[0]
body3 = face3.Parent
# Extrude 1 Edge
selection = Selection.Create(crv4)
pnt1 = crv4.Shape.StartPoint
result = ExtrudeEdges.Execute(selection, pnt1, Direction.DirZ, extrudeDepth, options)
face4 = result.CreatedFaces[0]
body4 = face4.Parent
# Fill
selection = Selection.Create([crv1,crv2,crv3,crv4])
secondarySelection = Selection.Empty()
options = FillOptions()
result = Fill.Execute(selection, secondarySelection, options, FillMode.ThreeD, None)
face5 = result.CreatedFaces[0]
body5 = face5.Parent
# Merge Bodies
targets = BodySelection.Create(body1)
tools = BodySelection.Create(body2, body3, body4, body5)
result = Combine.Merge(targets, tools)
# Fix Missing Face
options = FixMissingFacesOptions()
result = FixMissingFaces.FindAndFix(options, None)
#FixMissingFaces.FindAndFix does not return the faces creates. Knowing only one hole will be filled, get the last face in the list:
face6 = body1.Faces[-1] Â Â # Only body1 is left since all the other bodies were tool bodies to merge to body1
# Create Named Selection Group
primarySelection = BodySelection.Create(body1)
secondarySelection = Selection.Empty()
result = NamedSelection.Create(primarySelection, secondarySelection)
result = NamedSelection.Rename("Group1", "My Body1")
# Create Named Selection Group
primarySelection = FaceSelection.Create(face1)
secondarySelection = Selection.Empty()
result = NamedSelection.Create(primarySelection, secondarySelection)
result = NamedSelection.Rename("Group1", "My face1")
At the end, I still had the faces and body I could place in a named selection.
I did choose to use "Repair > Missing Faces" to make face6, and this does not return the face created, so I just got last face in the list. This is not the best means of creation if you wanted to track face6 to place in a named selection. It may be better to create all the wireframe edges of the body, and Fill or Blend each surface instead of pulling the 4 edges like I did so that you can track more explicitly all 6 faces made from the fill or blend operation.