23 Replies Latest reply on May 19, 2016 11:31 AM by Joe C.

    Inserting a library feature part into a part

    Nick Hayman

      Hello,

       

      I have a question about the macro i recorded a moment ago.

       

      The macro should take a library feature part and insert it onto a plane within a part. The reference point is the origin. So why does this not work?

       

      Im new to this so please go easy on me! Any help welcome. See code below:

       

      Sub main()

       

      Set swApp = _
      Application.SldWorks

       

      Set Part = swApp.ActiveDoc
      boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 1.45181545719948E-02, 0.01084930684622, 0, False, 0, Nothing, 0)
      boolstatus = Part.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
      Set Part = swApp.OpenDoc6("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\LIBS\NH4.SLDLFP", 1, 4, "", longstatus, longwarnings)
      Set Part = swApp.ActiveDoc
      Dim myModelView As Object
      Set myModelView = Part.ActiveView
      myModelView.FrameLeft = 0
      myModelView.FrameTop = 205
      Set myModelView = Part.ActiveView
      myModelView.FrameState = swWindowState_e.swWindowNormal
      swApp.ActivateDoc2 "NH4.SLDLFP", False, longstatus
      Set Part = swApp.ActiveDoc
      Set Part = swApp.ActiveDoc
      Set myModelView = Part.ActiveView
      myModelView.FrameLeft = 0
      myModelView.FrameTop = 21
      Set myModelView = Part.ActiveView
      myModelView.FrameState = swWindowState_e.swWindowMaximized
      swApp.ActivateDoc2 "PART1111", False, longstatus
      Set Part = swApp.ActiveDoc
      Set Part = swApp.ActiveDoc
      Set myModelView = Part.ActiveView
      myModelView.FrameLeft = 0
      myModelView.FrameTop = 205
      Set myModelView = Part.ActiveView
      myModelView.FrameState = swWindowState_e.swWindowMaximized
      Part.ClearSelection2 True
      boolstatus = Part.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
      boolstatus = Part.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
      Part.ClearSelection2 True
      Part.ClearSelection2 True
      boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 1.45181545719948E-02, 0.01084930684622, 0, False, 0, Nothing, 0)
      boolstatus = Part.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)
      boolstatus = Part.InsertLibraryFeature("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\LIBS\NH4.SLDLFP")
      Part.ClearSelection2 True
      Part.ClearSelection2 True
      boolstatus = Part.EditRebuild3()
      End Sub


        • Re: Inserting a library feature part into a part
          Simon Turner

          Your last SelectByID2 selects the origin, but I'm not sure that is taken into account by InsertLibraryFeature.

          I think you might have to select the Front Plane by using the Feature object, converting it to an Entity, calling Select4 with a SelectData object and finally setting the X, Y and Z values to 0.0

           

          Something like this:

           

          Dim myFeat As Feature

          Dim myEnt as Entity

          Dim mySelectData as SelectData

          Dim myPart as PartDoc

          Dim selMgr as SelectionManager

           

          Set myPart = Part

          Set myFeat = myPart.FeatureByName("Front Plane")

          Set myEnt = myFeat

           

          Set selMgr = Part.SelectionManager

          Set mySelectData = selMgr.CreateSelectDat

           

           

          mySelData.X = 0.0

          mySelData.Y = 0.0

          mySelData.Z = 0.0

           

          myEnt.Select4 False, mySelectData

           

          boolstatus = Part.InsertLibraryFeature("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\LIBS\NH4.SLDLFP")

            • Re: Inserting a library feature part into a part
              Nick Hayman

              Hello Simon,

               

              Thankyou for the reply. I accidentally deleted the macro in question and in order to keep things simple for myself i created a similar one. In this code there is no origin selection as i imported the file with 2 planes as references (not origin).

               

              The new macro does not work and im dont think your suggestion would either as the origin is not the problem? I could be wrong?

               

              Would appreciate it if you could look at the code again and reply with any suggestions? Thanks very kindly.

               

              Set Part = swApp.ActiveDoc
              boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", -0.075, 0.075, 0, False, 0, Nothing, 0)
              boolstatus = Part.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
              Set Part = swApp.OpenDoc6("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\Test Part Comunity\Test Part Library.SLDLFP", 1, 4, "", longstatus, longwarnings)
              Set Part = swApp.ActiveDoc
              Dim myModelView As Object
              Set myModelView = Part.ActiveView
              myModelView.FrameLeft = 0
              myModelView.FrameTop = 0
              Set myModelView = Part.ActiveView
              myModelView.FrameState = swWindowState_e.swWindowNormal
              swApp.ActivateDoc2 "Test Part Library.SLDLFP", False, longstatus
              Set Part = swApp.ActiveDoc
              Set Part = swApp.ActiveDoc
              Set myModelView = Part.ActiveView
              myModelView.FrameLeft = 0
              myModelView.FrameTop = 21
              Set myModelView = Part.ActiveView
              myModelView.FrameState = swWindowState_e.swWindowMaximized
              swApp.ActivateDoc2 "Test Part", False, longstatus
              Set Part = swApp.ActiveDoc
              Set Part = swApp.ActiveDoc
              Set myModelView = Part.ActiveView
              myModelView.FrameLeft = 0
              myModelView.FrameTop = 0
              Set myModelView = Part.ActiveView
              myModelView.FrameState = swWindowState_e.swWindowMaximized
              Part.ClearSelection2 True
              boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
              Part.ClearSelection2 True
              boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
              boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
              Part.ClearSelection2 True
              Part.ClearSelection2 True
              boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", -0.075, 0.075, 0, False, 0, Nothing, 0)
              boolstatus = Part.Extension.SelectByID2("Right Plane", "PLANE", 0, 0.075, 0, True, 1, Nothing, 0)
              boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", -0.075, 0, 0, True, 2, Nothing, 0)
              boolstatus = Part.InsertLibraryFeature("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\Test Part Comunity\Test Part Library.SLDLFP")
              Part.ClearSelection2 True
              Part.ClearSelection2 True
              boolstatus = Part.EditRebuild3()
              End Sub

                  • Re: Inserting a library feature part into a part
                    Nick Hayman

                    When i run the macro this is created. Notice model tree is empty. The property tab has a message (in red) Select entity to view or to modify its properties.

                     

                    Maybe this will shed some light why i does not work?

                      • Re: Inserting a library feature part into a part
                        Simon Turner

                        I think you should try the code I sent you. The plane needs selecting, and a location point supplied via the SelectData object.

                        You can use SelectByID2 to select the plane, but you must do so via coordinates, not the name. In your case, since you want to select the origin, you run the risk of selecting one of the other planes.

                          • Re: Inserting a library feature part into a part
                            Nick Hayman

                            Do you mean swapping the code that you sent with the selection code ID2? Replacing it at the start and the end?

                             

                            So as below:

                             

                            Sub main()

                             

                            Set swApp = _
                            Application.SldWorks

                             

                            Set Part = swApp.ActiveDoc
                            Dim myFeat As Feature
                            Dim myEnt As Entity
                            Dim mySelectData As SelectData
                            Dim myPart As PartDoc
                            Dim selMgr As SelectionManager
                            Set myPart = Part
                            Set myFeat = myPart.FeatureByName("Front Plane")
                            Set myEnt = myFeat
                            Set selMgr = Part.SelectionManager
                            Set mySelectData = selMgr.CreateSelectDat
                            mySelData.X = 0#
                            mySelData.Y = 0#
                            mySelData.Z = 0#
                            myEnt.Select4 False, mySelectData
                            Set Part = swApp.OpenDoc6("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\Test Part Comunity\Test Part Library.SLDLFP", 1, 4, "", longstatus, longwarnings)
                            Set Part = swApp.ActiveDoc
                            Dim myModelView As Object
                            Set myModelView = Part.ActiveView
                            myModelView.FrameLeft = 0
                            myModelView.FrameTop = 0
                            Set myModelView = Part.ActiveView
                            myModelView.FrameState = swWindowState_e.swWindowNormal
                            swApp.ActivateDoc2 "Test Part Library.SLDLFP", False, longstatus
                            Set Part = swApp.ActiveDoc
                            Set Part = swApp.ActiveDoc
                            Set myModelView = Part.ActiveView
                            myModelView.FrameLeft = 0
                            myModelView.FrameTop = 21
                            Set myModelView = Part.ActiveView
                            myModelView.FrameState = swWindowState_e.swWindowMaximized
                            swApp.ActivateDoc2 "Test Part", False, longstatus
                            Set Part = swApp.ActiveDoc
                            Set Part = swApp.ActiveDoc
                            Set myModelView = Part.ActiveView
                            myModelView.FrameLeft = 0
                            myModelView.FrameTop = 0
                            Set myModelView = Part.ActiveView
                            myModelView.FrameState = swWindowState_e.swWindowMaximized
                            Part.ClearSelection2 True
                            Dim myFeat As Feature
                            Dim myEnt As Entity
                            Dim mySelectData As SelectData
                            Dim myPart As PartDoc
                            Dim selMgr As SelectionManager
                            Set myPart = Part
                            Set myFeat = myPart.FeatureByName("Front Plane")
                            Set myEnt = myFeat
                            Set selMgr = Part.SelectionManager
                            Set mySelectData = selMgr.CreateSelectDat
                            mySelData.X = 0#
                            mySelData.Y = 0#
                            mySelData.Z = 0#
                            myEnt.Select4 False, mySelectData
                            boolstatus = Part.InsertLibraryFeature("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\Test Part Comunity\Test Part Library.SLDLFP")
                            Part.ClearSelection2 True
                            Part.ClearSelection2 True
                            boolstatus = Part.EditRebuild3()
                            End Sub

                             

                            It flags up Dim selMgr As SelectionManager as an error. User define type not defined.

                             

                            Sorry for late reply and im new to this so sorry for being slow!

                             

                            Any help greatly appreciated.

                             

                            Nick

                              • Re: Inserting a library feature part into a part
                                Simon Turner

                                First of all, make sure you have SldWorks Type Library enabled in Tools -> References (in the VBA Editor)

                                Same for SolidWorks Constant Type Library.

                                 

                                Secondly, you have duplicated a lot of the lines.

                                 

                                 

                                Sub main()

                                Dim myFeat As Feature
                                Dim myEnt As Entity
                                Dim mySelectData As SelectData
                                Dim myPart As PartDoc
                                Dim selMgr As SelectionManager

                                Dim libPart as ModelDoc2

                                 

                                Set swApp = Application.SldWorks

                                Set Part = swApp.ActiveDoc
                                Set myPart = Part
                                Set selMgr = Part.SelectionManager
                                Set mySelectData = selMgr.CreateSelectData


                                Set libPart = swApp.OpenDoc6("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\Test Part Comunity\Test Part Library.SLDLFP", 1, 4, "", longstatus, longwarnings)


                                Set Part = swApp.ActivateDoc2(Part.GetPathName, longstatus, longwarnings)
                                Set myFeat = myPart.FeatureByName("Front Plane")
                                Set myEnt = myFeat


                                mySelectData.X = 0#
                                mySelectData.Y = 0#
                                mySelectData.Z = 0#
                                myEnt.Select4 False, mySelectData
                                boolstatus = Part.InsertLibraryFeature("C:\ProgramData\SolidWorks\SolidWorks 2013\Design Library\Test Part Comunity\Test Part Library.SLDLFP")
                                Part.ClearSelection2 True
                                boolstatus = Part.EditRebuild3()
                                End Sub

                    • Re: Inserting a library feature part into a part
                      Joe C.

                      Hey guys, I'm having trouble with this topic in general, not necessarily macros so I do apologize if I'm posting in the incorrect place.

                      I have a DXF block of an aluminum extrusion, which is linked to my CAD block files in their respective folder. I brought the block in (on right plane) and extruded it. I have my "mesh/centerline" on the front plane which I use when mating to the assemblies and extrude, operations etc. I then created a sketch on the right plane which will be my notch, no dimensions for now so you can see on the attached print-screens. I tried saving the entire part and just the cut feature to the library. As you can see when I bring the cut back in on another part I cannot link the operation to the library. Am I missing something? I've been trying to figure this out for HOURS. Any help would be greatly appreciated.

                       

                      My reference points are Right Plane, Origin and ending vertex on Mesh line.

                       

                      THANKS TEAM!!!

                      Joe

                      7.PNG6.PNG5.PNG2.PNG1.PNG