We have an exciting announcement about badges coming in May 2025. Until then, we will temporarily stop issuing new badges for course completions and certifications. However, all completions will be recorded and fulfilled after May 2025.
3D Design

3D Design

Topics related to Ansys Discovery and Ansys SpaceClaim.

SpaceClaim Python API: Extruding Rectangle Sketch

    • Sterling Butters
      Subscriber

      Below is my code generated with the record feature:

      # Set New Sketch
      result = SketchHelper.StartConstraintSketching()
      # EndBlock
       
      # Sketch Rectangle
      point1 = Point2D.Create(MM(17),MM(-18))
      point2 = Point2D.Create(MM(-20),MM(-18))
      point3 = Point2D.Create(MM(-20),MM(24))
      result = SketchRectangle.Create(point1, point2, point3)
      # EndBlock
       
       
      # Solidify Sketch
      mode = InteractionMode.Solid
      result = ViewHelper.SetViewMode(mode, Info6)
      # EndBlock
       
      # Extrude 1 Face
      selection = Face1
      options = ExtrudeFaceOptions()
      options.KeepMirror = True
      options.KeepLayoutSurfaces = False
      options.KeepCompositeFaceRelationships = True
      options.PullSymmetric = False
      options.OffsetMode = OffsetMode.IgnoreRelationships
      options.Copy = False
      options.ForceDoAsExtrude = False
      options.ExtrudeType = ExtrudeType.Cut
      result = ExtrudeFaces.Execute(selection, MM(-21.37), options, Info7)
      # EndBlock
       
      I must say I really don't like the automatic creation of the metadata objects. In this case I would like to create an appropriate face/selection from SketchRectangle.Create(point1, point2, point3) that I can pass to ExtrudeFaces.Execute(selection, MM(-21.37), options) but I can't seem to figure out how to select that face. Please advise, thanks!
    • mjmiddle
      Ansys Employee

       

      You can set index selection method:

       

    • mjmiddle
      Ansys Employee

      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.

    • Sterling Butters
      Subscriber

      @mjmiddle Your first answer is really what I wanted. I can't believe I missed that setting. Thank you so much!

Viewing 3 reply threads
  • The topic ‘SpaceClaim Python API: Extruding Rectangle Sketch’ is closed to new replies.