8 Replies Latest reply on Feb 2, 2016 11:26 AM by roberto gennari

    Help for macro make TemporaryBody

    roberto gennari

      Good morning ,

      I have one thing to ask about temporary body,

      I would like to have a macro to create a temporary body selectable starting from a 3D sketch.

      The body is always a parallelepiped.

      Is it right if I use the API CreateBodyFromBox3?

      I don't know how to create the vector, whereas the sketch will often be in space, as in the attached

      Could you help me, please make it in writing a piece of code to create temporary body obtaining data from the sketch?

      I do not know how to get started

       

       

      I thank everyone in advance

       

      Roberto

        • Re: Help for macro make TemporaryBody
          roberto gennari

          Hallo,

          I tried to write a macro to be able to create the temporary body.

          The created body is unbalanced unfortunately, I think it is a problem of vector (see image).

           

          Can you tell me, if possible, where am I doing wrong?

          Attaching the part file for the test.

           

           

          This is the code:

           

          Thanks all

           

          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swModeler As SldWorks.Modeler
          Dim swBody As SldWorks.Body2
          Dim swFeat As SldWorks.Feature
          Dim swPart As SldWorks.PartDoc
          '
          Dim swMathUtil As SldWorks.MathUtility
          Dim swSelMgr As SldWorks.SelectionMgr
          Dim swSketchLine As SldWorks.SketchLine
          Dim oMyLine As Object
          Dim oMathVector As SldWorks.MathVector
          Dim oUnitVector As SldWorks.MathVector
          Dim vPt1 As Variant
          Dim vPt2 As Variant
          Dim dArr(2) As Double
          Dim i As Long
          Dim dArrUnit As Variant
          Dim swStartPt As SldWorks.SketchPoint
          Dim swEndPt As SldWorks.SketchPoint
          Dim swSketchSegment As SldWorks.SketchSegment
          Dim boolstatus As Boolean
          Dim Lenght1 As Double
          Dim Lenght2 As Double
          Dim Lenght3 As Double

             
          Sub main()

             
              Set swApp = Application.SldWorks
              If Not swApp Is Nothing Then
                  Set swModel = swApp.ActiveDoc
                  Set swMathUtil = swApp.GetMathUtility
                  If Not swModel Is Nothing And Not swMathUtil Is Nothing Then
                      Set swSelMgr = swModel.SelectionManager
                     
                      boolstatus = swModel.Extension.SelectByID2("Line1@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                     
                      Set swSketchSegment = swSelMgr.GetSelectedObject6(1, -1)
                      If Not swSketchSegment Is Nothing Then
                          Lenght1 = swSketchSegment.GetLength
                          swModel.ClearSelection2 True
                          Set swSketchSegment = Nothing
                      End If
                     
                      boolstatus = swModel.Extension.SelectByID2("Line2@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                     
                      Set swSketchSegment = swSelMgr.GetSelectedObject6(1, -1)
                      If Not swSketchSegment Is Nothing Then
                          Lenght2 = swSketchSegment.GetLength
                          swModel.ClearSelection2 True
                          Set swSketchSegment = Nothing
                      End If
                     
                      boolstatus = swModel.Extension.SelectByID2("Line10@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                     
                      Set swSketchSegment = swSelMgr.GetSelectedObject6(1, -1)
                      If Not swSketchSegment Is Nothing Then
                          Lenght3 = swSketchSegment.GetLength
                          Set swSketchSegment = Nothing
                      End If
                     
                      Set swSketchLine = swSelMgr.GetSelectedObject6(1, -1)
                      If Not swSketchLine Is Nothing Then
                          Set swEndPt = swSketchLine.GetEndPoint2
                          Set swStartPt = swSketchLine.GetStartPoint2
                          dArr(0) = swEndPt.X
                          dArr(1) = swEndPt.Y
                          dArr(2) = swEndPt.Z
                          vPt2 = dArr

                          dArr(0) = swStartPt.X
                          dArr(1) = swStartPt.Y
                          dArr(2) = swStartPt.Z
                          vPt1 = dArr

                          dArr(0) = vPt2(0) - vPt1(0)
                          dArr(1) = vPt2(1) - vPt1(1)
                          dArr(2) = vPt2(2) - vPt2(2)

                          Set oMathVector = swMathUtil.CreateVector(dArr)
                          If Not oMathVector Is Nothing Then
                              Set oUnitVector = oMathVector.Normalise()
                              dArrUnit = oUnitVector.ArrayData()
                              MsgBox ("Line vector is " & dArr(0) & "i + " & dArr(1) & "j + " & dArr(2) & "k" & vbCrLf & "Unit vector is " & dArrUnit(0) & "i + " & dArrUnit(1) & "j + " & dArrUnit(2) & "k")
                              Set oUnitVector = Nothing
                              Set oMathVector = Nothing
                          End If
                          Set oMyLine = Nothing
                          Set swSelMgr = Nothing
                      End If
                      Set swSketchLine = Nothing
                  End If
                  Set swMathUtil = Nothing
              End If
             
              Set swModeler = swApp.GetModeler
             
              Dim dblData(8) As Double
              dblData(0) = 101.70551678 / 1000 'Coordinate X of center
              dblData(1) = 23.0522269 / 1000   'Coordinate Y of center
              dblData(2) = 3.1578679 / 1000    'Coordinate Z of center
              dblData(3) = dArrUnit(0)
              dblData(4) = dArrUnit(1)
              dblData(5) = dArrUnit(2)
              dblData(6) = Lenght2     'boxWidth
              dblData(7) = Lenght1     'boxLenght
              dblData(8) = Lenght3     'boxHeight
             
              Set swBody = swModeler.CreateBodyFromBox3(dblData)
             
              Dim bValue As Boolean
              Dim bIsTempBody As Boolean
           
              bIsTempBody = swBody.IsTemporaryBody
              '
              Debug.Print "Is the loft body a temporary body?  " & bIsTempBody
              '
              bValue = swBody.Display3(swModel, RGB(255, 255, 0), swTempBodySelectable)

           

          Stop

           

           

          End Sub

            • Re: Help for macro make TemporaryBody
              Pankaj Bir

              Hey Roberto,

              You got most of the code right except for a small error in the computation of the axis.

              A small typographical error:

                          Set swSketchLine = swSelMgr.GetSelectedObject6(1, -1)

                          If Not swSketchLine Is Nothing Then

                              Set swEndPt = swSketchLine.GetEndPoint2

                              Set swStartPt = swSketchLine.GetStartPoint2

                              dArr(0) = swEndPt.X

                              dArr(1) = swEndPt.Y

                              dArr(2) = swEndPt.Z

                              vPt2 = dArr

                              dArr(0) = swStartPt.X
                              dArr(1) = swStartPt.Y
                              dArr(2) = swStartPt.Z
                              vPt1 = dArr

                              dArr(0) = vPt2(0) - vPt1(0)
                              dArr(1) = vPt2(1) - vPt1(1)
                              dArr(2) = vPt2(2) - vPt2(2)

              Change it to Vpt1(0).

               

              However this does not solve your problem completely.

              You have to actually use the Api method of CreateTrimmedSheet and then

              the CreateExtrudedBody .

              I have just included a few lines of code that can help you get started.

               

               

              SwPlanerSurf = SwModeler.CreatePlanarSurface2(Dpoint1, PlanerNormalVec, XDirVec)

                SwCurve(0) = SwModeler.CreateLine(Dpoint1, swCurveDir1)

                SwCurve(0) = SwCurve(0).CreateTrimmedCurve2(Dpoint1(0), Dpoint1(1), Dpoint1(2), Dpoint2(0), Dpoint2(1), Dpoint2(2))

                SwCurve(1) = SwModeler.CreateLine(Dpoint2, swCurveDir2)

                SwCurve(1) = SwCurve(1).CreateTrimmedCurve2(Dpoint2(0), Dpoint2(1), Dpoint2(2), Dpoint3(0), Dpoint3(1), Dpoint3(2))

                SwCurve(2) = SwModeler.CreateLine(Dpoint3, swCurveDir3)

                SwCurve(2) = SwCurve(2).CreateTrimmedCurve2(Dpoint3(0), Dpoint3(1), Dpoint3(2), Dpoint4(0), Dpoint4(1), Dpoint4(2))

                SwCurve(3) = SwModeler.CreateLine(Dpoint4, swCurveDir4)

                SwCurve(3) = SwCurve(3).CreateTrimmedCurve2(Dpoint4(0), Dpoint4(1), Dpoint4(2), Dpoint1(0), Dpoint1(1), Dpoint1(2))

                If Not SwCurve(0) Is Nothing And _

                Not SwCurve(1) Is Nothing And _

                 Not SwCurve(2) Is Nothing And _

                 Not SwCurve(3) Is Nothing Then

                SwProfileSurfaceBody = SwPlanerSurf.CreateTrimmedSheet(SwCurve)

                 End If

              If Not SwProfileSurfaceBody Is Nothing Then

                SwTempExtrudeBody = SwModeler.CreateExtrudedBody(SwProfileSurfaceBody, SwExtrudeDirVec, 5 / 1000)

                End If

               

               

              This will help to align the body completely with your 3d Sketch. I think that is what you want.

              The other Api method used doesn't get you there. You get the box in the plane but not aligned completely. More useful when you

              have things parallel to the principal axes.

               

              Hope this helps. If you need any further help just ask.

               

              Pankaj Bir

               

                • Re: Help for macro make TemporaryBody
                  roberto gennari

                  Hi Pankaj,

                  Thanks for your help and for making me understand the error,

                  I understand what is the right way to get the body properly oriented.

                  But I don't think I can get properly carriers as required by API,

                  can you hel me please?

                   

                  Thank you

                  Good day

                  • Re: Help for macro make TemporaryBody
                    roberto gennari

                    Hallo,

                    I started writing the code being able to obtain the following information:

                    - Starting point and end Line1, it also serves as a carrier for the X-axis surface

                    - Starting point and end Linea2-3-4-, I need to create lines of trimming of the surface

                    - Starting point and end Linea10, it also serves as vector Z axis for the surface, in addition to the starting point for the surface

                     

                     

                     

                    Once you get to the line

                     

                    Set planeSurf = swModeler.CreatePlanarSurface2((Line10_vPt1_Start), (dArrUnit_10), (dArrUnit_1))

                     

                     

                    I by mistake and do not understand where I'm wrong,

                    You could help you understand so you can get on?

                     

                    Thank you all

                     

                    Here the code:

                     

                    Dim swApp As SldWorks.SldWorks
                    Dim swModel As SldWorks.ModelDoc2
                    Dim swModeler As SldWorks.Modeler
                    Dim swBody As SldWorks.Body2
                    Dim swFeat As SldWorks.Feature
                    Dim swPart As SldWorks.PartDoc
                    '
                    Dim swMathUtil As SldWorks.MathUtility
                    Dim swSelMgr As SldWorks.SelectionMgr

                    Dim oMyLine As Object
                    Dim oMathVector As SldWorks.MathVector
                    Dim oUnitVector As SldWorks.MathVector
                    Dim vPt1 As Variant
                    Dim vPt2 As Variant
                    Dim dArr(2) As Double
                    Dim i As Long
                    Dim dArrUnit As Variant
                    Dim swStartPt As SldWorks.SketchPoint
                    Dim swEndPt As SldWorks.SketchPoint
                    Dim swSketchSegment As SldWorks.SketchSegment
                    Dim boolstatus As Boolean

                       
                    Sub main()

                       
                        Set swApp = Application.SldWorks
                        If Not swApp Is Nothing Then
                            Set swModel = swApp.ActiveDoc
                                swModel.ClearSelection2 True
                            Set swMathUtil = swApp.GetMathUtility
                            If Not swModel Is Nothing And Not swMathUtil Is Nothing Then
                                Set swSelMgr = swModel.SelectionManager
                               
                                'Linea1 ricavo punto iniziale e finale e il vettore per recuperlo per l'estrusione
                                boolstatus = swModel.Extension.SelectByID2("Line1@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                                '
                                Dim swSketchSegment_Line1 As SldWorks.SketchSegment
                                '
                                Set swSketchSegment_Line1 = swSelMgr.GetSelectedObject6(1, -1)
                                If Not swSketchSegment_Line1 Is Nothing Then
                                'Ricavo la lunghezza della Linea1
                                Dim Lenght_Line1 As Double
                                    Lenght_Line1 = swSketchSegment_Line1.GetLength
                                    '
                                Dim swSketchLine_Line1 As SldWorks.SketchLine
                                    Set swSketchLine_Line1 = swSelMgr.GetSelectedObject6(1, -1)
                                    '
                                    Dim Line1_swStartPt As SldWorks.SketchPoint
                                    Dim Line1_ As SldWorks.SketchPoint
                                    Dim Line1_vPt1_Start As Variant
                                    Dim Line1_vPt2_End As Variant
                                    Dim Line1_dArrEnd(2) As Double
                                    Dim Line1_dArrStart(2) As Double
                                    Set Line1_swStartPt = swSketchLine_Line1.GetStartPoint2
                                    Set Line1_swEndPt = swSketchLine_Line1.GetEndPoint2
                                    'Ricavo le coordinate dei punti Start e End della Linea1
                                    '
                                    'Punto Start Iniziale linea Linea1
                                    Line1_dArrStart(0) = Line1_swStartPt.X
                                    Line1_dArrStart(1) = Line1_swStartPt.Y
                                    Line1_dArrStart(2) = Line1_swStartPt.Z
                                    Line1_vPt1_Start = Line1_dArrStart
                                    '
                                    'Punto End finale linea Linea1
                                    Line1_dArrEnd(0) = Line1_swEndPt.X
                                    Line1_dArrEnd(1) = Line1_swEndPt.Y
                                    Line1_dArrEnd(2) = Line1_swEndPt.Z
                                    Line1_vPt2_End = Line1_dArrEnd
                                    '
                                    Debug.Print "Punto Iniziale Linea1"
                                    Debug.Print Line1_dArrStart(0) * 1000
                                    Debug.Print Line1_dArrStart(1) * 1000
                                    Debug.Print Line1_dArrStart(2) * 1000
                                    '
                                    Debug.Print "Punto Finale Linea1"
                                    Debug.Print Line1_dArrEnd(0) * 1000
                                    Debug.Print Line1_dArrEnd(1) * 1000
                                    Debug.Print Line1_dArrEnd(2) * 1000
                                    '
                                                    'Calcolo il vettore di direzione tramite StartPoint e EndPoint
                                    Dim oMyLine1 As Object
                                    Dim oMathVector_1 As SldWorks.MathVector
                                    Dim oUnitVector_1 As SldWorks.MathVector
                                    Dim dArr_1(2) As Double
                                    Dim ii As Long
                                    Dim dArrUnit_1 As Variant
                                    '
                                    dArr_1(0) = Line1_vPt2_End(0) - Line1_vPt1_Start(0)
                                    dArr_1(1) = Line1_vPt2_End(1) - Line1_vPt1_Start(1)
                                    dArr_1(2) = Line1_vPt2_End(2) - Line1_vPt1_Start(1)

                                    Set oMathVector_1 = swMathUtil.CreateVector(dArr_1)
                                    If Not oMathVector_1 Is Nothing Then
                                        Set oUnitVector_1 = oMathVector_1.Normalise()
                                        dArrUnit_1 = oUnitVector_1.ArrayData()
                                        'MsgBox ("Line vector is " & dArr_1(0) & "i + " & dArr_1(1) & "j + " & dArr_1(2) & "k" & vbCrLf & "Unit vector is " & dArrUnit_1(0) & "i + " & dArrUnit_1(1) & "j + " & dArrUnit_1(2) & "k")
                                    End If
                                    '
                                    swModel.ClearSelection2 True
                                    Set swSketchSegment_Line1 = Nothing
                                    Set swSketchLine_Line1 = Nothing
                                End If
                                 'Fine Linea1
                                 '
                                 '
                                 '
                                 '
                                'Linea2 ricavo punto iniziale e finale
                                boolstatus = swModel.Extension.SelectByID2("Line2@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                               
                    '
                                Dim swSketchSegment_Line2 As SldWorks.SketchSegment
                                '
                                Set swSketchSegment_Line2 = swSelMgr.GetSelectedObject6(1, -1)
                                If Not swSketchSegment_Line2 Is Nothing Then
                                'Ricavo la lunghezza della Linea2
                                Dim Lenght_Line2 As Double
                                    Lenght_Line2 = swSketchSegment_Line2.GetLength
                                    '
                                Dim swSketchLine_Line2 As SldWorks.SketchLine
                                    Set swSketchLine_Line2 = swSelMgr.GetSelectedObject6(1, -1)
                                    '
                                    Dim Line2_swStartPt As SldWorks.SketchPoint
                                    Dim Line2_ As SldWorks.SketchPoint
                                    Dim Line2_vPt1_Start As Variant
                                    Dim Line2_vPt2_End As Variant
                                    Dim Line2_dArrEnd(2) As Double
                                    Dim Line2_dArrStart(2) As Double
                                    Set Line2_swStartPt = swSketchLine_Line2.GetStartPoint2
                                    Set Line2_swEndPt = swSketchLine_Line2.GetEndPoint2
                                    'Ricavo le coordinate dei punti Start e End della Linea2
                                    '
                                    'Punto Iniziale linea Linea2
                                    Line2_dArrStart(0) = Line2_swStartPt.X
                                    Line2_dArrStart(1) = Line2_swStartPt.Y
                                    Line2_dArrStart(2) = Line2_swStartPt.Z
                                    Line2_vPt1_Start = Line2_dArrStart
                                    '
                                    'Punto finale linea Linea2
                                    Line2_dArrEnd(0) = Line2_swEndPt.X
                                    Line2_dArrEnd(1) = Line2_swEndPt.Y
                                    Line2_dArrEnd(2) = Line2_swEndPt.Z
                                    Line2_vPt2_End = Line2_dArrEnd
                                    '
                                    Debug.Print "Punto Iniziale Linea2"
                                    Debug.Print Line2_dArrStart(0) * 1000
                                    Debug.Print Line2_dArrStart(1) * 1000
                                    Debug.Print Line2_dArrStart(2) * 1000
                                    '
                                    Debug.Print "Punto Finale Linea2"
                                    Debug.Print Line2_dArrEnd(0) * 1000
                                    Debug.Print Line2_dArrEnd(1) * 1000
                                    Debug.Print Line2_dArrEnd(2) * 1000
                                    '
                                    swModel.ClearSelection2 True
                                    Set swSketchSegment_Line1 = Nothing
                                    Set swSketchLine_Line1 = Nothing
                                End If
                                 'Fine Linea2
                                 '
                                 '
                                 '
                                 '
                                'Linea3 ricavo punto iniziale e finale
                                boolstatus = swModel.Extension.SelectByID2("Line3@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                                '
                                Dim swSketchSegment_Line3 As SldWorks.SketchSegment
                                '
                                Set swSketchSegment_Line3 = swSelMgr.GetSelectedObject6(1, -1)
                                If Not swSketchSegment_Line3 Is Nothing Then
                                'Ricavo la lunghezza della Linea3
                                Dim Lenght_Line3 As Double
                                    Lenght_Line3 = swSketchSegment_Line3.GetLength
                                    '
                                Dim swSketchLine_Line3 As SldWorks.SketchLine
                                    Set swSketchLine_Line3 = swSelMgr.GetSelectedObject6(1, -1)
                                    '
                                    Dim Line3_swStartPt As SldWorks.SketchPoint
                                    Dim Line3_ As SldWorks.SketchPoint
                                    Dim Line3_vPt1_Start As Variant
                                    Dim Line3_vPt2_End As Variant
                                    Dim Line3_dArrEnd(2) As Double
                                    Dim Line3_dArrStart(2) As Double
                                    Set Line3_swStartPt = swSketchLine_Line3.GetStartPoint2
                                    Set Line3_swEndPt = swSketchLine_Line3.GetEndPoint2
                                    'Ricavo le coordinate dei punti Start e End della Linea3
                                    '
                                    'Punto Iniziale linea Linea3
                                    Line3_dArrStart(0) = Line3_swStartPt.X
                                    Line3_dArrStart(1) = Line3_swStartPt.Y
                                    Line3_dArrStart(2) = Line3_swStartPt.Z
                                    Line3_vPt1_Start = Line3_dArrStart
                                    '
                                    'Punto finale linea Linea3
                                    Line3_dArrEnd(0) = Line3_swEndPt.X
                                    Line3_dArrEnd(1) = Line3_swEndPt.Y
                                    Line3_dArrEnd(2) = Line3_swEndPt.Z
                                    Line3_vPt2_End = Line3_dArrEnd
                                    '
                                    Debug.Print "Punto Iniziale  Linea3"
                                    Debug.Print Line3_dArrStart(0) * 1000
                                    Debug.Print Line3_dArrStart(1) * 1000
                                    Debug.Print Line3_dArrStart(2) * 1000
                                   
                                    Debug.Print "Punto Finale Linea3"
                                    Debug.Print Line3_dArrEnd(0) * 1000
                                    Debug.Print Line3_dArrEnd(1) * 1000
                                    Debug.Print Line3_dArrEnd(2) * 1000
                                    '
                                    swModel.ClearSelection2 True
                                    Set swSketchSegment_Line3 = Nothing
                                    Set swSketchLine_Line3 = Nothing
                                End If
                                 'Fine Linea3
                                 '
                                 '
                                 '
                                 '
                                'Linea4 ricavo punto iniziale e finale
                                boolstatus = swModel.Extension.SelectByID2("Line4@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                                '
                                Dim swSketchSegment_Line4 As SldWorks.SketchSegment
                                '
                                Set swSketchSegment_Line4 = swSelMgr.GetSelectedObject6(1, -1)
                                If Not swSketchSegment_Line4 Is Nothing Then
                                'Ricavo la lunghezza della Linea3
                                Dim Lenght_Line4 As Double
                                    Lenght4 = swSketchSegment_Line4.GetLength
                                    '
                                Dim swSketchLine_Line4 As SldWorks.SketchLine
                                    Set swSketchLine_Line4 = swSelMgr.GetSelectedObject6(1, -1)
                                    '
                                    Dim Line4_swStartPt As SldWorks.SketchPoint
                                    Dim Line4_ As SldWorks.SketchPoint
                                    Dim Line4_vPt1_Start As Variant
                                    Dim Line4_vPt2_End As Variant
                                    Dim Line4_dArrEnd(2) As Double
                                    Dim Line4_dArrStart(2) As Double
                                    Set Line4_swStartPt = swSketchLine_Line4.GetStartPoint2
                                    Set Line4_swEndPt = swSketchLine_Line4.GetEndPoint2
                                    'Ricavo le coordinate dei punti Start e End della Linea4
                                    '
                                    'Punto Iniziale linea Linea4
                                    Line4_dArrStart(0) = Line4_swStartPt.X
                                    Line4_dArrStart(1) = Line4_swStartPt.Y
                                    Line4_dArrStart(2) = Line4_swStartPt.Z
                                    Line4_vPt1_Start = Line4_dArrStart
                                    '
                                    'Punto finale linea Linea4
                                    Line4_dArrEnd(0) = Line4_swEndPt.X
                                    Line4_dArrEnd(1) = Line4_swEndPt.Y
                                    Line4_dArrEnd(2) = Line4_swEndPt.Z
                                    Line4_vPt2_End = Line4_dArrEnd
                                    '
                                    Debug.Print "Punto Iniziale  Linea4"
                                    Debug.Print Line4_dArrStart(0) * 1000
                                    Debug.Print Line4_dArrStart(1) * 1000
                                    Debug.Print Line4_dArrStart(2) * 1000
                                   
                                    Debug.Print "Punto Finale Linea4"
                                    Debug.Print Line4_dArrEnd(0) * 1000
                                    Debug.Print Line4_dArrEnd(1) * 1000
                                    Debug.Print Line4_dArrEnd(2) * 1000
                                    '
                                    swModel.ClearSelection2 True
                                    Set swSketchSegment_Line4 = Nothing
                                    Set swSketchLine_Line4 = Nothing
                                End If
                                 'Fine Linea4
                                              '
                                 '
                                 '
                                 '
                                'Linea10 (Spessore)ricavo punto iniziale e finale e il vettore per recuperlo per l'estrusione
                                boolstatus = swModel.Extension.SelectByID2("Line10@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                                '
                                Dim swSketchSegment_Line10 As SldWorks.SketchSegment
                                '
                                Set swSketchSegment_Line10 = swSelMgr.GetSelectedObject6(1, -1)
                                If Not swSketchSegment_Line10 Is Nothing Then
                                'Ricavo la lunghezza della Linea10
                                Dim Lenght_Line10 As Double
                                    Lenght10 = swSketchSegment_Line10.GetLength
                                    '
                                Dim swSketchLine_Line10 As SldWorks.SketchLine
                                    Set swSketchLine_Line10 = swSelMgr.GetSelectedObject6(1, -1)
                                    '
                                    Dim Line10_swStartPt As SldWorks.SketchPoint
                                    Dim Line10_ As SldWorks.SketchPoint
                                    Dim Line10_vPt1_Start As Variant
                                    Dim Line10_vPt2_End As Variant
                                    Dim Line10_dArrEnd(2) As Double
                                    Dim Line10_dArrStart(2) As Double
                                    Set Line10_swStartPt = swSketchLine_Line10.GetStartPoint2
                                    Set Line10_swEndPt = swSketchLine_Line10.GetEndPoint2
                                    'Ricavo le coordinate dei punti Start e End della Linea10
                                    '
                                    'Punto Iniziale linea Linea10
                                    Line10_dArrStart(0) = Line10_swStartPt.X
                                    Line10_dArrStart(1) = Line10_swStartPt.Y
                                    Line10_dArrStart(2) = Line10_swStartPt.Z
                                    Line10_vPt1_Start = Line10_dArrStart
                                    '
                                    'Punto finale linea Linea10
                                    Line10_dArrEnd(0) = Line10_swEndPt.X
                                    Line10_dArrEnd(1) = Line10_swEndPt.Y
                                    Line10_dArrEnd(2) = Line10_swEndPt.Z
                                    Line10_vPt2_End = Line10_dArrEnd
                                    '
                                    Debug.Print "Punto Iniziale  Linea10"
                                    Debug.Print Line10_dArrStart(0) * 1000
                                    Debug.Print Line10_dArrStart(1) * 1000
                                    Debug.Print Line10_dArrStart(2) * 1000
                                   
                                    Debug.Print "Punto Finale Linea10"
                                    Debug.Print Line10_dArrEnd(0) * 1000
                                    Debug.Print Line10_dArrEnd(1) * 1000
                                    Debug.Print Line10_dArrEnd(2) * 1000
                                    '
                                    swModel.ClearSelection2 True
                                    Set swSketchSegment_Line10 = Nothing
                                    Set swSketchLine_Line10 = Nothing
                                    '
                                    '
                                    '
                                    'Calcolo il vettore di direzione tramite StartPoint e EndPoint
                                    Dim oMyLine10 As Object
                                    Dim oMathVector_10 As SldWorks.MathVector
                                    Dim oUnitVector_10 As SldWorks.MathVector
                                    Dim dArr_10(2) As Double
                                    Dim i As Long
                                    Dim dArrUnit_10 As Variant
                                    '
                                    dArr_10(0) = Line10_vPt2_End(0) - Line10_vPt1_Start(0)
                                    dArr_10(1) = Line10_vPt2_End(1) - Line10_vPt1_Start(1)
                                    dArr_10(2) = Line10_vPt2_End(2) - Line10_vPt1_Start(1)

                                    Set oMathVector_10 = swMathUtil.CreateVector(dArr_10)
                                    If Not oMathVector_10 Is Nothing Then
                                        Set oUnitVector_10 = oMathVector_10.Normalise()
                                        dArrUnit_10 = oUnitVector_10.ArrayData()
                                        'MsgBox ("Line vector is " & dArr_10(0) & "i + " & dArr_10(1) & "j + " & dArr_10(2) & "k" & vbCrLf & "Unit vector is " & dArrUnit_10(0) & "i + " & dArrUnit_10(1) & "j + " & dArrUnit_10(2) & "k")
                                    End If
                                    swModel.ClearSelection2 True
                                End If
                                 'Fine Linea10
                       
                                'create a planar surface
                       
                                Dim planeSurf As SldWorks.Surface
                                Dim trimCurves(3) As SldWorks.Curve
                       
                                Set planeSurf = swModeler.CreatePlanarSurface2((Line10_vPt1_Start), (dArrUnit_10), (dArrUnit_1))
                               
                                ''''Error
                                ''''
                                ''''

                            End If
                        End If

                    End Sub

                      • Re: Help for macro make TemporaryBody
                        Simon Turner

                        You forgot to set swModeler:

                         

                        Set swModeler = swApp.GetModeler

                          • Re: Help for macro make TemporaryBody
                            roberto gennari

                            Hallo,

                            I went on writing the code, but now I am the problem in the trimming of the surface

                             

                              profileBody = planeSurf.CreateTrimmedSheet (trimCurves)

                             

                            It return to the Object Nothing, I think the problem is upstream, in the creation of the flat surface

                             

                            planeSurf swModeler.CreatePlanarSurface2 = ((Line10_vPt1_Start), (dArrUnit_10), (dArrUnit_1))

                             

                            Could you help me resolve this problem please?

                             

                            Thank you all

                             

                            I reported the code for convenience in VB.net:

                             

                             

                            Imports SolidWorks.Interop.sldworks
                            Imports SolidWorks.Interop.swconst
                             
                                Dim swApp As SldWorks
                                Dim swModel As ModelDoc2
                                Dim swModeler As Modeler
                                Dim swBody As Body2
                                Dim swFeat As Feature
                                Dim swPart As PartDoc
                                Dim planeSurf As Surface
                                Dim trimCurves(3) As Curve
                                '
                                Dim swMathUtil As MathUtility
                                Dim swSelMgr As SelectionMgr
                                Dim retval As Integer

                                '

                                Dim oMyLine As Object
                                Dim oMathVector As MathVector
                                Dim oUnitVector As MathVector
                                Dim vPt1 As Object
                                Dim vPt2 As Object
                                Dim dArr(2) As Double
                                Dim i As Long
                                Dim dArrUnit As Object
                                Dim swStartPt As SketchPoint
                                Dim swEndPt As SketchPoint
                                Dim swSketchSegment As SketchSegment
                                Dim boolstatus As Boolean
                                '
                                Dim oMyLine1 As Object
                                Dim oMathVector_1 As MathVector
                                Dim oUnitVector_1 As MathVector
                                Dim dArr_1(2) As Double
                                Dim ii As Long
                                Dim dArrUnit_1 As Object
                                Dim Line1_swStartPt As SketchPoint
                                Dim Line1_swEndPt As SketchPoint
                                Dim Line1_vPt1_Start As Object
                                Dim Line1_vPt2_End As Object
                                Dim Line1_dArrEnd(2) As Double
                                Dim Line1_dArrStart(2) As Double
                                '
                                Dim Line2_swStartPt As SketchPoint
                                Dim Line2_swEndPt As SketchPoint
                                Dim Line2_vPt1_Start As Object
                                Dim Line2_vPt2_End As Object
                                Dim Line2_dArrEnd(2) As Double
                                Dim Line2_dArrStart(2) As Double
                                '
                                Dim Line3_swStartPt As SketchPoint
                                Dim Line3_swEndPt As SketchPoint
                                Dim Line3_vPt1_Start As Object
                                Dim Line3_vPt2_End As Object
                                Dim Line3_dArrEnd(2) As Double
                                Dim Line3_dArrStart(2) As Double
                                '
                                Dim Line4_swStartPt As SketchPoint
                                Dim Line4_swEndPt As SketchPoint
                                Dim Line4_vPt1_Start As Object
                                Dim Line4_vPt2_End As Object
                                Dim Line4_dArrEnd(2) As Double
                                Dim Line4_dArrStart(2) As Double
                                '
                                Dim Line10_swStartPt As SketchPoint
                                Dim Line10_swEndPt As SketchPoint
                                Dim Line10_vPt1_Start As Object
                                Dim Line10_vPt2_End As Object
                                Dim Line10_dArrEnd(2) As Double
                                Dim Line10_dArrStart(2) As Double
                                '
                                Dim oMyLine10 As Object
                                Dim oMathVector_10 As MathVector
                                Dim oUnitVector_10 As MathVector
                                Dim dArr_10(2) As Double
                                Dim dArrUnit_10 As Object
                                Dim Lenght_Line10 As Double

                                Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

                                    swApp = CreateObject("SldWorks.Application")

                                    If Not swApp Is Nothing Then
                                        swModel = swApp.ActiveDoc
                                        swModel.ClearSelection2(True)
                                        '
                                        swModeler = swApp.GetModeler
                                        '
                                        swMathUtil = swApp.GetMathUtility
                                        If Not swModel Is Nothing And Not swMathUtil Is Nothing Then
                                            swSelMgr = swModel.SelectionManager

                                            'Linea1 ricavo punto iniziale e finale e il vettore per recuperlo per l'estrusione
                                            boolstatus = swModel.Extension.SelectByID2("Line1@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                                            '
                                            Dim swSketchSegment_Line1 As SketchSegment
                                            '
                                            swSketchSegment_Line1 = swSelMgr.GetSelectedObject6(1, -1)
                                            If Not swSketchSegment_Line1 Is Nothing Then
                                                'Ricavo la lunghezza della Linea1
                                                Dim Lenght_Line1 As Double
                                                Lenght_Line1 = swSketchSegment_Line1.GetLength
                                                '
                                                Dim swSketchLine_Line1 As SketchLine
                                                swSketchLine_Line1 = swSelMgr.GetSelectedObject6(1, -1)
                                                '
                                                Line1_swStartPt = swSketchLine_Line1.GetStartPoint2
                                                Line1_swEndPt = swSketchLine_Line1.GetEndPoint2
                                                'Ricavo le coordinate dei punti Start e End della Linea1
                                                '
                                                'Punto Start Iniziale linea Linea1
                                                Line1_dArrStart(0) = Line1_swStartPt.X
                                                Line1_dArrStart(1) = Line1_swStartPt.Y
                                                Line1_dArrStart(2) = Line1_swStartPt.Z
                                                Line1_vPt1_Start = Line1_dArrStart
                                                '
                                                'Punto End finale linea Linea1
                                                Line1_dArrEnd(0) = Line1_swEndPt.X
                                                Line1_dArrEnd(1) = Line1_swEndPt.Y
                                                Line1_dArrEnd(2) = Line1_swEndPt.Z
                                                Line1_vPt2_End = Line1_dArrEnd
                                                '
                                                Debug.Print("Punto Iniziale Linea1")
                                                Debug.Print(Line1_dArrStart(0) * 1000)
                                                Debug.Print(Line1_dArrStart(1) * 1000)
                                                Debug.Print(Line1_dArrStart(2) * 1000)
                                                '
                                                Debug.Print("Punto Finale Linea1")
                                                Debug.Print(Line1_dArrEnd(0) * 1000)
                                                Debug.Print(Line1_dArrEnd(1) * 1000)
                                                Debug.Print(Line1_dArrEnd(2) * 1000)
                                                '
                                                'Calcolo il vettore di direzione tramite StartPoint e EndPoint

                                                '
                                                dArr_1(0) = Line1_vPt2_End(0) - Line1_vPt1_Start(0)
                                                dArr_1(1) = Line1_vPt2_End(1) - Line1_vPt1_Start(1)
                                                dArr_1(2) = Line1_vPt2_End(2) - Line1_vPt1_Start(1)

                                                oMathVector_1 = swMathUtil.CreateVector(dArr_1)
                                                If Not oMathVector_1 Is Nothing Then
                                                    oUnitVector_1 = oMathVector_1.Normalise()
                                                    dArrUnit_1 = oUnitVector_1.ArrayData()
                                                    'MsgBox ("Line vector is " & dArr_1(0) & "i + " & dArr_1(1) & "j + " & dArr_1(2) & "k" & vbCrLf & "Unit vector is " & dArrUnit_1(0) & "i + " & dArrUnit_1(1) & "j + " & dArrUnit_1(2) & "k")
                                                End If
                                                '
                                                swModel.ClearSelection2(True)
                                                swSketchSegment_Line1 = Nothing
                                                swSketchLine_Line1 = Nothing
                                            End If
                                            'Fine Linea1
                                            '
                                            '
                                            '
                                            '
                                            'Linea2 ricavo punto iniziale e finale
                                            boolstatus = swModel.Extension.SelectByID2("Line2@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

                                            '
                                            Dim swSketchSegment_Line2 As SketchSegment
                                            '
                                            swSketchSegment_Line2 = swSelMgr.GetSelectedObject6(1, -1)
                                            If Not swSketchSegment_Line2 Is Nothing Then
                                                'Ricavo la lunghezza della Linea2
                                                Dim Lenght_Line2 As Double
                                                Lenght_Line2 = swSketchSegment_Line2.GetLength
                                                '
                                                Dim swSketchLine_Line2 As SketchLine
                                                swSketchLine_Line2 = swSelMgr.GetSelectedObject6(1, -1)
                                                '
                                                Line2_swStartPt = swSketchLine_Line2.GetStartPoint2
                                                Line2_swEndPt = swSketchLine_Line2.GetEndPoint2
                                                'Ricavo le coordinate dei punti Start e End della Linea2
                                                '
                                                'Punto Iniziale linea Linea2
                                                Line2_dArrStart(0) = Line2_swStartPt.X
                                                Line2_dArrStart(1) = Line2_swStartPt.Y
                                                Line2_dArrStart(2) = Line2_swStartPt.Z
                                                Line2_vPt1_Start = Line2_dArrStart
                                                '
                                                'Punto finale linea Linea2
                                                Line2_dArrEnd(0) = Line2_swEndPt.X
                                                Line2_dArrEnd(1) = Line2_swEndPt.Y
                                                Line2_dArrEnd(2) = Line2_swEndPt.Z
                                                Line2_vPt2_End = Line2_dArrEnd
                                                '
                                                Debug.Print("Punto Iniziale Linea2")
                                                Debug.Print(Line2_dArrStart(0) * 1000)
                                                Debug.Print(Line2_dArrStart(1) * 1000)
                                                Debug.Print(Line2_dArrStart(2) * 1000)
                                                '
                                                Debug.Print("Punto Finale Linea2")
                                                Debug.Print(Line2_dArrEnd(0) * 1000)
                                                Debug.Print(Line2_dArrEnd(1) * 1000)
                                                Debug.Print(Line2_dArrEnd(2) * 1000)
                                                '
                                                swModel.ClearSelection2(True)
                                                swSketchSegment_Line1 = Nothing
                                                'swSketchLine_Line1 = Nothing
                                            End If
                                            'Fine Linea2
                                            '
                                            '
                                            '
                                            '
                                            'Linea3 ricavo punto iniziale e finale
                                            boolstatus = swModel.Extension.SelectByID2("Line3@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                                            '
                                            Dim swSketchSegment_Line3 As SketchSegment
                                            '
                                            swSketchSegment_Line3 = swSelMgr.GetSelectedObject6(1, -1)
                                            If Not swSketchSegment_Line3 Is Nothing Then
                                                'Ricavo la lunghezza della Linea3
                                                Dim Lenght_Line3 As Double
                                                Lenght_Line3 = swSketchSegment_Line3.GetLength
                                                '
                                                Dim swSketchLine_Line3 As SketchLine
                                                swSketchLine_Line3 = swSelMgr.GetSelectedObject6(1, -1)
                                                '
                                                Line3_swStartPt = swSketchLine_Line3.GetStartPoint2
                                                Line3_swEndPt = swSketchLine_Line3.GetEndPoint2
                                                'Ricavo le coordinate dei punti Start e End della Linea3
                                                '
                                                'Punto Iniziale linea Linea3
                                                Line3_dArrStart(0) = Line3_swStartPt.X
                                                Line3_dArrStart(1) = Line3_swStartPt.Y
                                                Line3_dArrStart(2) = Line3_swStartPt.Z
                                                Line3_vPt1_Start = Line3_dArrStart
                                                '
                                                'Punto finale linea Linea3
                                                Line3_dArrEnd(0) = Line3_swEndPt.X
                                                Line3_dArrEnd(1) = Line3_swEndPt.Y
                                                Line3_dArrEnd(2) = Line3_swEndPt.Z
                                                Line3_vPt2_End = Line3_dArrEnd
                                                '
                                                Debug.Print("Punto Iniziale  Linea3")
                                                Debug.Print(Line3_dArrStart(0) * 1000)
                                                Debug.Print(Line3_dArrStart(1) * 1000)
                                                Debug.Print(Line3_dArrStart(2) * 1000)

                                                Debug.Print("Punto Finale Linea3")
                                                Debug.Print(Line3_dArrEnd(0) * 1000)
                                                Debug.Print(Line3_dArrEnd(1) * 1000)
                                                Debug.Print(Line3_dArrEnd(2) * 1000)
                                                '
                                                swModel.ClearSelection2(True)
                                                swSketchSegment_Line3 = Nothing
                                                swSketchLine_Line3 = Nothing
                                            End If
                                            'Fine Linea3
                                            '
                                            '
                                            '
                                            '
                                            'Linea4 ricavo punto iniziale e finale
                                            boolstatus = swModel.Extension.SelectByID2("Line4@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                                            '
                                            Dim swSketchSegment_Line4 As SketchSegment
                                            '
                                            swSketchSegment_Line4 = swSelMgr.GetSelectedObject6(1, -1)
                                            If Not swSketchSegment_Line4 Is Nothing Then
                                                'Ricavo la lunghezza della Linea3
                                                Dim Lenght_Line4 As Double
                                                Lenght_Line4 = swSketchSegment_Line4.GetLength
                                                '
                                                Dim swSketchLine_Line4 As SketchLine
                                                swSketchLine_Line4 = swSelMgr.GetSelectedObject6(1, -1)
                                                '
                                                Line4_swStartPt = swSketchLine_Line4.GetStartPoint2
                                                Line4_swEndPt = swSketchLine_Line4.GetEndPoint2
                                                'Ricavo le coordinate dei punti Start e End della Linea4
                                                '
                                                'Punto Iniziale linea Linea4
                                                Line4_dArrStart(0) = Line4_swStartPt.X
                                                Line4_dArrStart(1) = Line4_swStartPt.Y
                                                Line4_dArrStart(2) = Line4_swStartPt.Z
                                                Line4_vPt1_Start = Line4_dArrStart
                                                '
                                                'Punto finale linea Linea4
                                                Line4_dArrEnd(0) = Line4_swEndPt.X
                                                Line4_dArrEnd(1) = Line4_swEndPt.Y
                                                Line4_dArrEnd(2) = Line4_swEndPt.Z
                                                Line4_vPt2_End = Line4_dArrEnd
                                                '
                                                Debug.Print("Punto Iniziale  Linea4")
                                                Debug.Print(Line4_dArrStart(0) * 1000)
                                                Debug.Print(Line4_dArrStart(1) * 1000)
                                                Debug.Print(Line4_dArrStart(2) * 1000)

                                                Debug.Print("Punto Finale Linea4")
                                                Debug.Print(Line4_dArrEnd(0) * 1000)
                                                Debug.Print(Line4_dArrEnd(1) * 1000)
                                                Debug.Print(Line4_dArrEnd(2) * 1000)
                                                '
                                                swModel.ClearSelection2(True)
                                                swSketchSegment_Line4 = Nothing
                                                swSketchLine_Line4 = Nothing
                                            End If
                                            'Fine Linea4
                                            '
                                            '
                                            '
                                            '
                                            'Linea10 (Spessore)ricavo punto iniziale e finale e il vettore per recuperlo per l'estrusione
                                            boolstatus = swModel.Extension.SelectByID2("Line10@Schizzo3D1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                                            '
                                            Dim swSketchSegment_Line10 As SketchSegment
                                            '
                                            swSketchSegment_Line10 = swSelMgr.GetSelectedObject6(1, -1)
                                            If Not swSketchSegment_Line10 Is Nothing Then
                                                'Ricavo la lunghezza della Linea10

                                                Lenght_Line10 = swSketchSegment_Line10.GetLength
                                                '
                                                Dim swSketchLine_Line10 As SketchLine
                                                swSketchLine_Line10 = swSelMgr.GetSelectedObject6(1, -1)
                                                '
                                                Line10_swStartPt = swSketchLine_Line10.GetStartPoint2
                                                Line10_swEndPt = swSketchLine_Line10.GetEndPoint2
                                                'Ricavo le coordinate dei punti Start e End della Linea10
                                                '
                                                'Punto Iniziale linea Linea10
                                                Line10_dArrStart(0) = Line10_swStartPt.X
                                                Line10_dArrStart(1) = Line10_swStartPt.Y
                                                Line10_dArrStart(2) = Line10_swStartPt.Z
                                                Line10_vPt1_Start = Line10_dArrStart
                                                '
                                                'Punto finale linea Linea10
                                                Line10_dArrEnd(0) = Line10_swEndPt.X
                                                Line10_dArrEnd(1) = Line10_swEndPt.Y
                                                Line10_dArrEnd(2) = Line10_swEndPt.Z
                                                Line10_vPt2_End = Line10_dArrEnd
                                                '
                                                Debug.Print("Punto Iniziale  Linea10")
                                                Debug.Print(Line10_dArrStart(0) * 1000)
                                                Debug.Print(Line10_dArrStart(1) * 1000)
                                                Debug.Print(Line10_dArrStart(2) * 1000)

                                                Debug.Print("Punto Finale Linea10")
                                                Debug.Print(Line10_dArrEnd(0) * 1000)
                                                Debug.Print(Line10_dArrEnd(1) * 1000)
                                                Debug.Print(Line10_dArrEnd(2) * 1000)
                                                '
                                                swModel.ClearSelection2(True)
                                                swSketchSegment_Line10 = Nothing
                                                swSketchLine_Line10 = Nothing
                                                '
                                                '
                                                '
                                                'Calcolo il vettore di direzione tramite StartPoint e EndPoint

                                                '
                                                dArr_10(0) = Line10_vPt2_End(0) - Line10_vPt1_Start(0)
                                                dArr_10(1) = Line10_vPt2_End(1) - Line10_vPt1_Start(1)
                                                dArr_10(2) = Line10_vPt2_End(2) - Line10_vPt1_Start(1)

                                                oMathVector_10 = swMathUtil.CreateVector(dArr_10)
                                                If Not oMathVector_10 Is Nothing Then
                                                    oUnitVector_10 = oMathVector_10.Normalise()
                                                    dArrUnit_10 = oUnitVector_10.ArrayData()
                                                    'MsgBox ("Line vector is " & dArr_10(0) & "i + " & dArr_10(1) & "j + " & dArr_10(2) & "k" & vbCrLf & "Unit vector is " & dArrUnit_10(0) & "i + " & dArrUnit_10(1) & "j + " & dArrUnit_10(2) & "k")
                                                End If
                                                swModel.ClearSelection2(True)
                                            End If
                                            'Fine Linea10

                                            'create a planar surface

                                            '
                                            planeSurf = swModeler.CreatePlanarSurface2((Line10_vPt1_Start), (dArrUnit_10), (dArrUnit_1))
                                            '
                                            '
                                            Dim dDir_Line1(2) As Double
                                            dDir_Line1(0) = Line1_dArrEnd(0) - Line1_dArrStart(0) : dDir_Line1(1) = Line1_dArrEnd(1) - Line1_dArrStart(1) : dDir_Line1(2) = Line1_dArrEnd(2) - Line1_dArrStart(2)
                                            trimCurves(0) = swModeler.CreateLine(Line1_dArrStart, dDir_Line1)
                                            trimCurves(0) = trimCurves(0).CreateTrimmedCurve2(Line1_dArrStart(0), Line1_dArrStart(1), Line1_dArrStart(2), Line1_dArrEnd(0), Line1_dArrEnd(1), Line1_dArrEnd(2))
                                            'Wirebody
                                            swBody = trimCurves(0).CreateWireBody()
                                            'swPart.SetLineStyle "CHAIN"
                                            swPart = swApp.ActiveDoc
                                            retval = swBody.Display3(swPart, 255, swTempBodySelectOptions_e.swTempBodySelectable)

                                            '
                                            If trimCurves(0) Is Nothing Then
                                                MsgBox("Error")
                                            End If
                                            '
                                            Dim dDir_Line2(2) As Double
                                            dDir_Line2(0) = Line2_dArrEnd(0) - Line2_dArrStart(0) : dDir_Line2(1) = Line2_dArrEnd(1) - Line2_dArrStart(1) : dDir_Line2(2) = Line2_dArrEnd(2) - Line2_dArrStart(2)
                                            trimCurves(1) = swModeler.CreateLine(Line2_dArrStart, dDir_Line2)
                                            trimCurves(1) = trimCurves(1).CreateTrimmedCurve2(Line2_dArrStart(0), Line2_dArrStart(1), Line2_dArrStart(2), Line2_dArrEnd(0), Line2_dArrEnd(1), Line2_dArrEnd(2))
                                            '
                                            swBody = trimCurves(1).CreateWireBody()
                                            'swPart.SetLineStyle "CHAIN"
                                            swPart = swApp.ActiveDoc
                                            retval = swBody.Display3(swPart, 255, swTempBodySelectOptions_e.swTempBodySelectable)
                                            '
                                            If trimCurves(1) Is Nothing Then
                                                MsgBox("Error")
                                            End If
                                            '
                                            Dim dDir_Line3(2) As Double
                                            dDir_Line3(0) = Line3_dArrEnd(0) - Line3_dArrStart(0) : dDir_Line3(1) = Line3_dArrEnd(1) - Line3_dArrStart(1) : dDir_Line3(2) = Line3_dArrEnd(2) - Line3_dArrStart(2)
                                            trimCurves(2) = swModeler.CreateLine(Line3_dArrStart, dDir_Line3)
                                            trimCurves(2) = trimCurves(2).CreateTrimmedCurve2(Line3_dArrStart(0), Line3_dArrStart(1), Line3_dArrStart(2), Line3_dArrEnd(0), Line3_dArrEnd(1), Line3_dArrEnd(2))
                                            '
                                            swBody = trimCurves(2).CreateWireBody()
                                            'swPart.SetLineStyle "CHAIN"
                                            swPart = swApp.ActiveDoc
                                            retval = swBody.Display3(swPart, 255, swTempBodySelectOptions_e.swTempBodySelectable)
                                            '
                                            If trimCurves(2) Is Nothing Then
                                                MsgBox("Error")
                                            End If
                                            '
                                            Dim dDir_Line4(2) As Double
                                            dDir_Line4(0) = Line4_dArrEnd(0) - Line4_dArrStart(0) : dDir_Line4(1) = Line4_dArrEnd(1) - Line4_dArrStart(1) : dDir_Line4(2) = Line4_dArrEnd(2) - Line4_dArrStart(2)
                                            trimCurves(3) = swModeler.CreateLine(Line4_dArrStart, dDir_Line4)
                                            trimCurves(3) = trimCurves(3).CreateTrimmedCurve2(Line4_dArrStart(0), Line4_dArrStart(1), Line4_dArrStart(2), Line4_dArrEnd(0), Line4_dArrEnd(1), Line4_dArrEnd(2))
                                            '
                                            swBody = trimCurves(3).CreateWireBody()
                                            'swPart.SetLineStyle "CHAIN"
                                            swPart = swApp.ActiveDoc
                                            retval = swBody.Display3(swPart, 255, swTempBodySelectOptions_e.swTempBodySelectable)
                                            '
                                            If trimCurves(2) Is Nothing Then
                                                MsgBox("Error")
                                            End If
                                            '

                                            Dim profileBody As Body2
                                            If Not trimCurves(0) Is Nothing And Not trimCurves(1) Is Nothing And Not trimCurves(2) Is Nothing And Not trimCurves(3) Is Nothing Then

                                          'Here the error
                                                profileBody = planeSurf.CreateTrimmedSheet(trimCurves)
                                            End If
                                            '
                                            If profileBody Is Nothing Then
                                                MsgBox("Error")
                                            End If

                                            Dim dirVector As MathVector
                                            Dim extrudedBody As Body2

                                            extrudedBody = swModeler.CreateExtrudedBody(profileBody, dArrUnit_10, Lenght_Line10)
                                            extrudedBody.Display3(swModel, RGB(1, 0, 0), swTempBodySelectOptions_e.swTempBodySelectOptionNone)

                                        End If
                                    End If

                              • Re: Help for macro make TemporaryBody
                                Simon Turner

                                I don't know why your call didn't work, but I got CreatePlanarTrimSurfaceDLL to work instead.

                                I made an array of 12 doubles representing the four corners of the rectangle.

                                Then these lines worked for me:

                                 

                                                    Dim retB As Boolean

                                                    swBody = swPart.CreateNewBody

                                                    retB = swBody.CreatePlanarTrimSurfaceDLL((pts), (dArrUnit_10))

                                                    retB = swBody.CreateBodyFromSurfaces()

                                  • Re: Help for macro make TemporaryBody
                                    roberto gennari

                                    Hi Simon,

                                    many thanks for your help, I tried the APIs that were advising me and it works well.

                                    I still managed to understand what was the error in my code and it was on the creation of the temporary surface.

                                    The code was correct bat the direction of the dArrUnit_1 (axis X) was inverse, so the cutting of the surface was empty.

                                     

                                    planeSurf = swModeler.CreatePlanarSurface2((Line10_vPt1_Start), (dArrUnit_10), (dArrUnit_1))

                                     

                                    Thank you very much again for your time

                                     

                                    Roberto