5 Replies Latest reply on Aug 17, 2014 12:22 PM by Sanya Shmidt

    Make component virtual

    Sanya Shmidt

      Hello guys,

       

      Can someone tell me how to make a newly added part Virtual?

       

      Here is a pretty simple code that adds a VirtualBlank part in the assembly. I just need  to make it virtual. And if anyone knows how to rename it.

       

      Dim swApp As Object

      Dim bool As Boolean

      Sub Virtual()

      Set swApp = Application.SldWorks

      Set swAssemblyModel = swApp.ActiveDoc

      ' Check to see if a document is loaded

      If swAssemblyModel.GetType <> 2 Then

        swApp.SendMsgToUser ("Please make sure assembly model is open and active.")

          GoTo docleanup

      End If

      bool = swAssemblyModel.AddComponent("w:\SolidWorks Files\SWFiles\Parts\VirtualBlank.SLDPRT", 1, 1, 1)

       

      ' Make the component virtual

      'stat = bool.MakeVirtual

       

      docleanup:

                 Set swApp = Nothing

      End Sub

       

      Can someone help?

      Thank you,

      Alex.

          • Re: Make component virtual
            Sanya Shmidt

            Deepak,

             

            thank you.

             

            I looked at the suggested code. I don`t understand why should I select the face of the part first? Is it possible to skip that part and just add a virtual component?

            The code below works but user needs to select planar face....? why?

             

            Option Explicit

            Dim swApp As SldWorks.SldWorks

            Dim swModel As SldWorks.ModelDoc2

            Dim swAssy As SldWorks.AssemblyDoc

            Dim swComponent As SldWorks.Component2

            Dim swSelMgr As SldWorks.SelectionMgr

            Dim status As Long

             

            Sub Main()

             

                Set swApp = Application.SldWorks

             

                Set swModel = swApp.ActiveDoc

                Set swAssy = swModel

             

                ' Get the pre-selected planar face  ??????????????? Is there a way to pre-select any planar face?

                Dim swFeature  As SldWorks.Face2

                Set swSelMgr = swModel.SelectionManager

                Set swFeature = swSelMgr.GetSelectedObject6(1, 0)

             

                ' Create the part and insert it as a virtual component

                ' in the assembly

                status = swAssy.InsertNewVirtualPart(swFeature, swComponent)

             

                swModel.ClearSelection2 (True)

             

            End Sub

             

             

            Actually...... I think I can use this code :

            boolstatus = swAssy.Extension.SelectByID2("", "FACE", 0, 0, 0, False, 0, Nothing, 0)

             

            to preselect a face. Right?

             

            Deepak is there any way to rename virtual component or insert with predefined name???

             

             

            Thank you,

              • Re: Make component virtual
                Sanya Shmidt

                Got it.

                 

                Here is the piece of code that creates Virtual Component and renames it. Can be done via user Input as well.

                 

                Option Explicit

                Sub main()

                    Dim swApp As SldWorks.SldWorks

                    Set swApp = Application.SldWorks

                       

                    Dim swModel As SldWorks.ModelDoc2

                    Set swModel = swApp.ActiveDoc

                   

                    Dim swSelMgr As SldWorks.SelectionMgr

                    Set swSelMgr = swModel.SelectionManager

                   

                    If swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0) = False Then

                        Debug.Print "Failed to select Front plane; check feature name."

                        Exit Sub

                    End If

                   

                    Dim swPlaneFeature As SldWorks.Feature

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

                    Dim swPlane As SldWorks.RefPlane

                    Set swPlane = swPlaneFeature.GetSpecificFeature2

                   

                    Dim swAssem As SldWorks.AssemblyDoc

                    Set swAssem = swModel

                   

                    Dim lResult As Long

                    Dim swVirtComp As SldWorks.Component2

                    swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swExtRefUpdateCompNames, False

                    lResult = swAssem.InsertNewVirtualPart(swPlane, swVirtComp)

                    swVirtComp.Name2 = "1234"

                    

                End Sub

                  • Re: Make component virtual
                    Deepak Gupta

                    The reason macro was asking a face/plane selected so that it can use that as a placement plane for the new part. Similar to what you do when you insert a part in context of the assembly.

                     

                    You can use a pop up box to ask user for the component name and then substitute that in the codes. Here are the codes you can use:

                     

                    Option Explicit

                        Dim swApp As SldWorks.SldWorks

                        Dim swModel As SldWorks.ModelDoc2

                        Dim swAssy As SldWorks.AssemblyDoc

                        Dim swSelMgr As SldWorks.SelectionMgr

                        Dim lResult As Long

                        Dim swVirtComp As SldWorks.Component2

                        Dim swPlaneFeature As SldWorks.Feature

                        Dim swPlane As SldWorks.RefPlane

                        Dim sName As String

                       

                    Sub main()

                       

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                        Set swAssy = swModel

                       

                        Set swSelMgr = swModel.SelectionManager

                       

                        sName = InputBox(Prompt:="Add Component Name Here", _

                              Title:="ENTER COMPONENT NAME", Default:="Add Component Name Here")

                             

                              If sName = "Add Component Name Here" Or sName = vbNullString Then

                                MsgBox "Please Enter Component Name first!"

                               Exit Sub

                        Else

                      

                        If swAssy.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0) = False Then

                            Debug.Print "Failed to select Front plane; check feature name."

                            Exit Sub

                        End If

                       

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

                        Set swPlane = swPlaneFeature.GetSpecificFeature2

                      

                        swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swExtRefUpdateCompNames, False

                        lResult = swAssy.InsertNewVirtualPart(swPlane, swVirtComp)

                        swVirtComp.Name2 = sName

                       

                        End If

                       

                    End Sub

                     

                    You may also set the codes such that user will have to input the part name and can't even press cancel.

                    cName:

                           sName = InputBox(Prompt:="Add Component Name Here", _

                              Title:="ENTER COMPONENT NAME", Default:="Add Component Name Here")

                             

                              If sName = "Add Component Name Here" Or sName = vbNullString Then

                                MsgBox "Please Enter Component Name first!"

                              Goto cName

                        Else

                      

                       

                      • Re: Make component virtual
                        Sanya Shmidt

                        Yes, thank you. Deepak. The other question I have but not sure how to do is: How I can access custom properties of the virtual part from assembly level. I can use the following code to add properties to an assembly :

                         

                        Dim retval As String

                        If swVirtComp.CustomInfo2("", "Description") = "" Then

                             retval = swVirtComp.AddCustomInfo3("", "Description2", swCustomInfoText, "")

                        End If

                         

                        How can I assign/create properties to a virtual component? after I add it

                         

                        lResult = swAssy.InsertNewVirtualPart(swPlane, swVirtComp)

                        swVirtComp.Name2 = sName

                        swVirtComp.????????????

                         

                        Thank you.