Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 532

Split Feature without SplitFeature

$
0
0

By Xiaodong Liang

SplitFeature is also a type of feature. But if you do not want to split a feature without SplitFeature, you could calcuate the new profiles and create the new splitting features. The following is a small code which converts an extrude feature to two extrude features with a middle line, and also creates a surface of the middle line to represent the split plane.

Assume the orignal extrude feature is based on a profile of a rectangle lines.

image

Sub test()

    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument
   
    Dim oDef As PartComponentDefinition
    Set oDef = oPartDoc.ComponentDefinition
   
    Dim oOldF As ExtrudeFeature
    Set oOldF = oDef.Features.ExtrudeFeatures(1)
   
    Dim oOldFDef As ExtrudeDefinition
    Set oOldFDef = oOldF.Definition
   
    Dim oPath As ProfilePath
    Set oPath = oOldFDef.Profile(1)
   
    Dim oSketchEnt As SketchEntity
    Dim oSketch As PlanarSketch
  
        
        Dim oSketchLine1 As SketchLine
        Set oSketchLine1 = oPath(1).SketchEntity
       
        Dim oSketchLine2 As SketchLine
        Set oSketchLine2 = oPath(2).SketchEntity
       
        Dim oSketchLine3 As SketchLine
        Set oSketchLine3 = oPath(3).SketchEntity
       
        Dim oSketchLine4 As SketchLine
        Set oSketchLine4 = oPath(4).SketchEntity
       
        'get sketch
        Set oSketch = oSketchLine1.Parent
       
        'get mid pt of line 1
        Dim oMidPtOfLine1 As Point2d
        Set oMidPtOfLine1 = oSketchLine1.Geometry.MidPoint
       
        'add sketch mid pt
        Dim oMidSketchPtLine1 As SketchPoint
        Set oMidSketchPtLine1 = oSketch.SketchPoints.Add(oMidPtOfLine1)
        Call oSketch.GeometricConstraints.AddCoincident(oMidSketchPtLine1, oSketchLine1)
       
        'get mid pt of line3 which is parallel to line 1
        Dim oMidPtOfLine3 As Point2d
        Set oMidPtOfLine3 = oSketchLine3.Geometry.MidPoint
       
        'add sketch mid pt
         Dim oMidSketchPtLine3 As SketchPoint
        Set oMidSketchPtLine3 = oSketch.SketchPoints.Add(oMidPtOfLine3)
         Call oSketch.GeometricConstraints.AddCoincident(oMidSketchPtLine3, oSketchLine3)
       
        ' create mid line
        Dim oMiddleLine As SketchLine
        Set oMiddleLine = oSketch.SketchLines.AddByTwoPoints(oMidPtOfLine1, oMidPtOfLine3)
     
        Call oMiddleLine.StartSketchPoint.Merge(oMidSketchPtLine1)
        Call oMiddleLine.EndSketchPoint.Merge(oMidSketchPtLine3)
       
        ' create new extrude 1: just change the old feature's profile
        Dim oNewPaths1 As ObjectCollection
        Set oNewPaths1 = ThisApplication.TransientObjects.CreateObjectCollection
        oNewPaths1.Add oSketchLine1
        oNewPaths1.Add oSketchLine2
        oNewPaths1.Add oSketchLine3
        oNewPaths1.Add oMiddleLine
       
        Dim oNewProfile1 As Profile
        Set oNewProfile1 = oSketch.Profiles.AddForSolid(False, oNewPaths1)

        oOldFDef.Profile = oNewProfile1
       
        'create the other extrude feature
         Dim oNewPaths2 As ObjectCollection
        Set oNewPaths2 = ThisApplication.TransientObjects.CreateObjectCollection
        oNewPaths2.Add oSketchLine1
        oNewPaths2.Add oSketchLine4
        oNewPaths2.Add oSketchLine3
        oNewPaths2.Add oMiddleLine
       
        Dim oNewProfile2 As Profile
        Set oNewProfile2 = oSketch.Profiles.AddForSolid(False, oNewPaths2)
        
        Dim oNewExtrudeDef As ExtrudeDefinition
        Set oNewExtrudeDef = oDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oNewProfile2, kJoinOperation)
        Call oNewExtrudeDef.SetDistanceExtent(1, kNegativeExtentDirection)
        Dim oNewExtrude As ExtrudeFeature
        Set oNewExtrude = oDef.Features.ExtrudeFeatures.Add(oNewExtrudeDef)
       
        'create surface on
          Dim oNewPaths3 As ObjectCollection
        Set oNewPaths3 = ThisApplication.TransientObjects.CreateObjectCollection
        oNewPaths3.Add oMiddleLine
       
        Dim oNewProfile3 As Profile
        Set oNewProfile3 = oSketch.Profiles.AddForSurface(oMiddleLine)
       
         Dim oSurfaceDef As ExtrudeDefinition
        Set oSurfaceDef = oDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oNewProfile3, kSurfaceOperation)
        Call oSurfaceDef.SetDistanceExtent(10, kSymmetricExtentDirection)
        Dim oSurface As ExtrudeFeature
        Set oSurface = oDef.Features.ExtrudeFeatures.Add(oSurfaceDef)

   
End Sub

 

After the code, the feature becomes:

image


Viewing all articles
Browse latest Browse all 532

Latest Images

Trending Articles



Latest Images