3 Replies Latest reply on Nov 2, 2017 3:53 PM by Adrian Luparu

    open part/assembly of selected from drawing

    Adrian Luparu

      I have found couple of days searching for some code and now I am unable to find it anymore, so I use this forum to ask if there is a code in c# to open the selected part/model of from drawing. Thank you.

        • Re: open part/assembly of selected from drawing
          Josh Brady
          Dim swApp As SldWorks.SldWorks
          Dim Part As SldWorks.ModelDoc2
          Dim boolstatus As Boolean
          Dim longstatus As Long, longwarnings As Long
          Dim FeatureData As Object
          Dim Feature As Object
          Dim Component As Object
          Dim SelMgr As SldWorks.SelectionMgr
          Dim MyDwgComp As SldWorks.DrawingComponent
          Dim MyComp As SldWorks.Component2
          Dim Part2Open As SldWorks.ModelDoc2
          Dim myView As SldWorks.View
          Dim modRefByView As SldWorks.ModelDoc2
          Dim dummy As Long
          Sub main()
          'On Error GoTo ERROROUT
          Set swApp = Application.SldWorks
          Set Part = swApp.ActiveDoc
          Set SelMgr = Part.SelectionManager
          If Part.GetType = swDocNONE Or Part.GetType = swDocPART Then
              MsgBox "Open Assembly or Drawing for this function"
              Exit Sub
          End If
          If Part.GetType = swDocDRAWING Then
              If SelMgr.GetSelectedObjectType2(1) = swSelDRAWINGVIEWS Then
                  Set myView = SelMgr.GetSelectedObject5(1)
                  Set modRefByView = myView.ReferencedDocument
                  If modRefByView.GetType = swDocPART Then
                      Set Part2Open = swApp.ActivateDoc2(modRefByView.GetPathName, False, dummy)
                  ElseIf modRefByView.GetType = swDocASSEMBLY Then
                      If SelMgr.GetSelectedObjectCount = 2 Then
                          Set MyDwgComp = SelMgr.GetSelectedObjectsComponent2(2)
                          Set MyComp = MyDwgComp.Component
                          Set Part2Open = swApp.ActivateDoc2(MyComp.GetPathName, False, dummy)
                      End If
                  End If
              ElseIf SelMgr.GetSelectedObjectType3(1, -1) = swSelCOMPONENTS Then
                  Set MyDwgComp = SelMgr.GetSelectedObject6(1, -1)
                  Set MyComp = MyDwgComp.Component
                  Set Part2Open = swApp.ActivateDoc2(MyComp.GetPathName, False, dummy)
              End If
          Else
              Set MyComp = SelMgr.GetSelectedObjectsComponent2(1)
              Part.ClearSelection2 True
              MyComp.Select2 False, 1
              Part.OpenCompFile
              swApp.ActiveDoc.Visible = True
          End If
          ERROROUT:
          End Sub
          

          You can translate this.

          • Re: open part/assembly of selected from drawing
            Adrian Luparu

            For my particular purpose voila in c#

             

             

                private void openSelectedPart()
                {
                    SldWorks swApp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));
                    ModelDoc2 swModel = default(ModelDoc2);
                    ModelDoc2 modRefByView = default(ModelDoc2);
                    ModelDoc2 Part2Open = default(ModelDoc2);
                    SelectionMgr swSelMgr = default(SelectionMgr);

             

                    SolidWorks.Interop.sldworks.View swView;

             

                    int dummy = 0;

             

             
                    if (swApp.GetDocumentCount() == 0)
                    {
                        return;
                    }
                    swModel = (ModelDoc2)swApp.ActiveDoc;

             

                
                    if (swModel.GetType() != (int)swDocumentTypes_e.swDocDRAWING)
                    {
                        MessageBox.Show("You Must Run This Macro Inside Of A Drawing");
                        return;
                    }

             

                    swSelMgr = (SelectionMgr)swModel.SelectionManager;
                    swView = (SolidWorks.Interop.sldworks.View)swSelMgr.GetSelectedObject5(1);
                    modRefByView = swView.ReferencedDocument;

             

                    if (modRefByView.GetType() == (int)swDocumentTypes_e.swDocPART)
                    {
                        Part2Open = (ModelDoc2)swApp.ActivateDoc2(modRefByView.GetPathName(), false, dummy);
                    }

             

                    else if (modRefByView.GetType() == (int)swDocumentTypes_e.swDocASSEMBLY)
                    {

             

                    }

             

             

             

                }