8 Replies Latest reply on Nov 4, 2018 1:01 AM by Artem Taturevych

    Loft surface using sketch segments

    Marco Maso

      Hello again, I have a series of sketch with several sketch segments inside. I would like to create a simple macro that, after selecting two sketch segments, let me quickly create a surface loft.

       

      Unfortunately I can't get InsertLoftRefSurface2 to work, no matter what.

       

      At the moment I simply select two sketch segments in the interface, then launch the macro. The macro:

       

      1. Saves the two selected sketch segments in an array

      2. Clears the selection

      3. Using ModelDoc2 selects the two sketch segments again using SelectByID2 with mark 1 as per InsertLoftRefSurface2 API remarks

      4. If both selections are true (and they are) performs a InsertLoftRefSurface2(false, false, false, 1.0, 0, 0)

       

      Solidworks spins for a second then nothing happens. No errors, no clues, nothing.

       

      I registered a macro doing the same within the interface. The main difference is that the macro selects the two sketch segments using SelectByID2("Unknow", "SELOBJGROUP", [etc.]) with mark 1.

       

      I was also trying to create a temporary body by using Modeler.CreateLoftBody2 but I can't get InsertLoftRefSurface2 to work, let alone CreateLoftBody2.

       

      Is there something I'm not aware of?

        • Re: Loft surface using sketch segments
          Fifi Riri

          Could you post your macro's code?

            • Re: Loft surface using sketch segments
              Marco Maso

              Absolutely, here's the relevant code.

               

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swModelExtension As SldWorks.ModelDocExtension

              Dim swSelectionManager As SldWorks.SelectionMgr

              Dim selStatus As Boolean

               

              ' Precondition: two sketch segments selected in the interface

               

               

               

              Sub main()

               

               

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swModelExtension = swModel.Extension

              Set swSelectionManager = swModel.SelectionManager

               

               

               

              If swSelectionManager.GetSelectedObjectCount2(-1) = 2 Then

                 

                  Dim sketchSegment1 As SldWorks.SketchSegment

                  Dim sketchSegment2 As SldWorks.SketchSegment

                 

                  Dim sketchFeature1 As SldWorks.Feature

                  Dim sketchFeature2 As SldWorks.Feature

                 

                  Set sketchSegment1 = swSelectionManager.GetSelectedObject6(1, -1)

                  Set sketchSegment2 = swSelectionManager.GetSelectedObject6(2, -1)

                 

                  Set sketchFeature1 = sketchSegment1.GetSketch()

                  Set sketchFeature2 = sketchSegment2.GetSketch()

                 

                  swModel.ClearSelection2 (True)

                 

                  Debug.Print "ID #1: " & sketchSegment1.GetName() & "@" & sketchFeature1.Name

                  Debug.Print "ID #2: " & sketchSegment2.GetName() & "@" & sketchFeature2.Name

                 

                  selStatus = swModelExtension.SelectByID2(sketchSegment1.GetName() & "@" & sketchFeature1.Name, "EXTSKETCHSEGMENT", 0, 0, 0, False, 1, Nothing, 0)

                  Debug.Print "First sketch segment selection status: " & selStatus

                 

                  selStatus = swModelExtension.SelectByID2(sketchSegment2.GetName() & "@" & sketchFeature2.Name, "EXTSKETCHSEGMENT", 0, 0, 0, True, 1, Nothing, 0)

                  Debug.Print "Second sketch segment selection status: " & selStatus

                 

                  swModel.InsertLoftRefSurface2 False, True, False, 1, 0, 0

                    

              End If

               

               

              End Sub

                • Re: Loft surface using sketch segments
                  Fifi Riri

                  The macro you posted works fine for me.

                  And this works just as well:

                   

                  Option Explicit

                  Dim swApp As SldWorks.SldWorks

                  Dim swModel As SldWorks.ModelDoc2

                  Dim swSelectionManager As SldWorks.SelectionMgr

                  Sub main()

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swSelectionManager = swModel.SelectionManager

                  If swSelectionManager.GetSelectedObjectCount2(-1) = 2 Then

                      swModel.InsertLoftRefSurface2 False, True, False, 1, 0, 0

                  End If

                  End Sub

                   

                  It might depends of the sketch segments themselves.

                  Could you post your file?

                  • Re: Loft surface using sketch segments
                    Artem Taturevych

                    That most likely incorrect selection points on the profile. Like the one below (blue points):

                    You need to use ISelectionMgr::SetSelectionPoint2 to set the points.

                     

                    But better approach is to use ISketchSegment::Select4 method instead of SelectByID2 and set the selection point via ISelectData::X, ISelectData::Y and ISelectData::Z properties for the selection data in the second parameter.

                      • Re: Loft surface using sketch segments
                        Marco Maso

                        Excuse me, could you point to me how to correctly use ISelectData? Because I tried but I still don't get anything.

                         

                            swModel.ClearSelection2 True

                           

                            Set swSelectData = swSelectionManager.CreateSelectData

                           

                            swSelectData.Mark = 1

                            swSelectData.X = skPtMt1.ArrayData(0)

                            swSelectData.Y = skPtMt1.ArrayData(1)

                            swSelectData.Z = skPtMt1.ArrayData(2)

                            sketchSegment1.Select4 True, swSelectData

                         

                            swSelectData.X = skPtMt2.ArrayData(0)

                            swSelectData.Y = skPtMt2.ArrayData(1)

                            swSelectData.Z = skPtMt2.ArrayData(2)

                            sketchSegment2.Select4 True, swSelectData

                           

                            swModel.InsertLoftRefSurface2 False, True, False, 1, 0, 0

                         

                        skPtMt1 and skPtMt2 are the right side points. I also tried making a line between them and I can confirm they are correct and aligned for loft uses.

                         

                        I also tried:

                         

                            Set swSelectData = swSelectionManager.CreateSelectData

                            swSelectData.Mark = 1

                           

                            selStatus = sketchSegment1.Select4(True, swSelectData)

                            selStatus = swSelectionManager.SetSelectionPoint2(1, -1, skPtMt1.ArrayData(0), skPtMt1.ArrayData(1), skPtMt1.ArrayData(2))

                            selStatus = sketchSegment2.Select4(True, swSelectData)

                            selStatus = swSelectionManager.SetSelectionPoint2(1, -1, skPtMt2.ArrayData(0), skPtMt2.ArrayData(1), skPtMt2.ArrayData(2))

                           

                             'selStatus is true / true / true / true

                         

                            swModel.InsertLoftRefSurface2 False, True, False, 1, 0, 0

                         

                        Just to clarify here's what I'm trying to achieve:

                         

                        1.jpg

                         

                        2.jpg

                         

                        One small thing that irks me is that I don't get any kind of feedback on what I'm doing. You need to perfectly know how the API works because SW won't give you any error code, am I correct?