15 Replies Latest reply on Sep 12, 2016 11:49 PM by Mr Omkar Deshpande

    To insert new part at sub-assembly level

    Mr Omkar Deshpande

      Hi all,

       

          Is there any way that i can insert a new part (top-down) at sub-assembly level?

          swAssemblyDoc.InsertNewPart2 works, but it is at top level. I want to do it at sub-assembly level. (I need to use the sketch in another sub-assembly as reference; hence i cannot open that sub-assembly)

       

      Thanks in advance.

        • Re: To insert new part at sub-assembly level
          Jesse Seger

          After you insert the component, you can use IAssemblyDoc::ReorganizeComponents to move the component to the sub assembly.

          • Re: To insert new part at sub-assembly level
            Keith Rice

            Edit the sub-assembly in context and then use this API call. For example, select a face within the edited sub-assembly and run this:

             

            Dim swApp As SldWorks.SldWorks
            Dim swModel As SldWorks.ModelDoc2
            Dim swAssy As SldWorks.AssemblyDoc
            Dim swSelMgr As SldWorks.SelectionMgr
            Dim swFace As SldWorks.Face2
            
            Sub main()
                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc
                Set swAssy = swModel
                Set swSelMgr = swModel.SelectionManager
                Set swFace = swSelMgr.GetSelectedObject6(1, -1)
                swAssy.InsertNewPart2 "C:\test.sldprt", swFace
            End Sub
            

             

            Your parenthetical statement might be saying that this isn't an option for you, if so, I'll just post it for reference, anyway.

             

            Keith

            SolidWorks API Training and Services

              • Re: To insert new part at sub-assembly level
                Mr Omkar Deshpande

                Thanks for reply;

                This code is working for me at top level. I need to do it at sub-assembly level.

                But I am not getting how to edit the sub-assembly in context. I tried EditAssembly & EditPart2 both but no luck.

                  • Re: To insert new part at sub-assembly level
                    Keith Rice

                    The code I provided does work at the sub-assembly level. You just need to have that sub-assembly edited first.

                     

                    Please note, your original question was simply, "How do I get IAssemblyDoc::InsertNewPart2 to work at the sub-assembly level?" And the answer, as I demonstrated, is to simply have the sub-assembly edited in-context before running that API call.

                     

                    Nevertheless, it appears that you want a macro that will demonstrate how to select sub-assembles in context as well. Here is a macro that will work on any sub-assembly or the top-level assembly. You will need to select a face first before running it. After it runs, it should create a sketch with a circle in it. Please also note that the new part is being created on the C:\ (see NEW_PART_PATH constant). As Artem mentioned, this might be a problem if you don't have write access.

                     

                    Const NEW_PART_PATH As String = "C:\test.sldprt"
                    
                    Dim swApp As SldWorks.SldWorks
                    Dim swModel As SldWorks.ModelDoc2
                    Dim swAssy As SldWorks.AssemblyDoc
                    Dim swSelMgr As SldWorks.SelectionMgr
                    Dim swFace As SldWorks.Face2
                    Dim swComp As SldWorks.Component2
                    Dim swParentComp As SldWorks.Component2
                    
                    Sub main()
                        Set swApp = Application.SldWorks
                        Set swModel = swApp.ActiveDoc
                        
                        If swModel Is Nothing Then Exit Sub
                        If swModel.GetType <> swDocASSEMBLY Then Exit Sub
                        
                        Set swAssy = swModel
                        Set swSelMgr = swModel.SelectionManager
                        
                        If swSelMgr.GetSelectedObjectType3(1, -1) <> swSelFACES Or swSelMgr.GetSelectedObjectCount2(-1) <> 1 Then
                            MsgBox "Please select a single face."
                            Exit Sub
                        End If
                        
                        Set swFace = swSelMgr.GetSelectedObject6(1, -1)
                        Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
                        Set swParentComp = swComp.GetParent
                        
                        If swParentComp Is Nothing Then
                            swComp.Select False
                        Else
                            swParentComp.Select False
                        End If
                        
                        swAssy.EditAssembly
                        swFace.Select False
                        swAssy.InsertNewPart2 NEW_PART_PATH, swFace
                        swModel.SketchManager.InsertSketch True
                        swModel.SketchManager.CreateCircleByRadius 0, 0, 0, 0.01
                        swAssy.EditAssembly
                    End Sub
                    

                     

                    Keith

                    SolidWorks API Training and Services

                • Re: To insert new part at sub-assembly level
                  Deepak Gupta

                  Try these codes. These work only one level down. Also you must select a face of the child component of that sub assy.

                   

                  Option Explicit
                  Sub main()
                      Dim swApp                      As SldWorks.SldWorks
                      Dim swModel                    As SldWorks.ModelDoc2
                      Dim swSelMgr                    As SldWorks.SelectionMgr
                      Dim swConfigMgr                As SldWorks.ConfigurationManager
                      Dim swConf                      As SldWorks.Configuration
                      Dim swComp                      As SldWorks.Component2
                      Dim swParentComp                As SldWorks.Component2
                      Dim swFace                      As SldWorks.Face2
                     
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Set swSelMgr = swModel.SelectionManager
                      Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
                      Set swFace = swSelMgr.GetSelectedObject6(1, -1)
                     
                      Set swParentComp = swComp.GetParent
                      If Nothing Is swParentComp Then
                          Set swConfigMgr = swModel.ConfigurationManager
                          Set swConf = swConfigMgr.ActiveConfiguration
                          Set swParentComp = swConf.GetRootComponent
                      End If
                  
                      swModel.Extension.SelectByID2 swParentComp.Name2 & "@" & swModel.GetTitle, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0
                      swModel.EditAssembly
                      swModel.InsertNewPart2 "C:\Test Part.SLDPRT", swFace
                      swModel.SketchManager.InsertSketch True
                      swModel.EditAssembly
                      swModel.ClearSelection2 True
                  
                  End Sub