3 Replies Latest reply on Sep 11, 2015 3:13 AM by Tapani Sjoman

    OpenDoc6 error

    Raphael Rabbi Lage Freitas

      Hello everyone. I am a new user of solidworks API with some programming background. I actually have been able to create some useful macros with success. But I am struggling with one problem that I cant seem to find a solution. I hope you guys can help me.

       

      I have attached the macro so you guys can have a better look at the coding. I am sorry about the number of comments, but as I said as a new user I have to do a lot of experimentation before ger everything up and running smoothly.

       

      So let me try to explain my struggle to you.

       

      I have created a macro that check which parts of an assembly do not have its respective drawings (Knowing that the drawings have the same name of the parts and are located in the same folder. So when I run the command for the buttom Open Part it just dont work. After some hours of trying to find a solution I realised that the long warning variable was returning 128, which means that the part is open. The thing is: I have JUST the assembly document opened. If I create a new macro and run just the bit of OpenDoc6 it works normally. But if I have the Assembly opened and try to run the command it gives the warning and nothing happens. What I really dont understand is that it worked before, and I changed some other parts of the code (The create drawings command if I am not mistaken) and then it suddely would work anymore.

       

      I will paste right here the command buttom code and will attach the full macro as well. Hope anyone can understand this, because after some hours spent trying to solve I really am sutcked and cant move on.

       

      Thanks in advance.

       

      Sub OpenSelectedPart(bViewOnly As Boolean)

       

       

      Dim fileerror   As Long

      Dim filewarning As Long

      Dim sSelectPath As String

      Dim sSelectName As String

      Dim sSelection  As String

      Dim strRefConfig   As String

      Dim sDocType        As String

      Dim sCompConfig  As String

       

       

         

      'Get full path of selected part from list box

         ' sSelectPath = listMissingDrawings.Column(2, listMissingDrawings.ListIndex)

         

          'Get full path of selected part from list box

          sSelectPath = listMissingDrawings.Column(2, listMissingDrawings.ListIndex)

          sSelectName = listMissingDrawings.Column(1, listMissingDrawings.ListIndex)

          sCompFileName = Mid(sSelectPath, InStrRev(sSelectPath, "\") + 1)

          Debug.Print sCompFileName

      ' Get Used Config

          sSelection = GetSelectionName(sSelectName, sRootCompName)

          boolstatus = swAssem.SelectByID(sSelection, "COMPONENT", 0#, 0#, 0#)

          Set swSelPart = swSelMgr.GetSelectedObject(1)

          'strRefConfig = swSelPart.ReferencedConfiguration

          sDocType = LCase(Mid(sSelectPath, Len(sSelectPath) - 6))

          Debug.Print sSelectPath

          Debug.Print sDocType

         

          swApp.OpenDoc6 sSelectPath, swDocPART, swOpenDocOptions_Silent, sCompConfig, fileerror, filewarning

          If filewarning = 128 Then

          MsgBox "Component is already opened!"

          End If

          If sDocType = ".sldprt" Then

          swApp.OpenDoc6 sSelectPath, swDocPART, swOpenDocOptions_ReadOnly, sCompConfig, fileerror, filewarning

          Else

          swApp.OpenDoc2 sSelectPath, swDocASSEMBLY, bViewOnly, bViewOnly, True, fileerror

          End If

         

      End Sub

        • Re: OpenDoc6 error
          Tapani Sjoman

          All resolved components are opened at the same time when their assembly is opened. They just are not visible. Maybe you want to activate selected component insted of opening?

           

          swApp.ActivateDoc2 sSelectPath, True, Errors

            • Re: OpenDoc6 error
              Raphael Rabbi Lage Freitas

              Thanks very much Tapani. It worked this way. Now Iam just having another issue but its not to do with open doc. Maybe someone can help me. I have another command buttom which create a drawing with the 3 views from the part I am opening, but the thing is. After creating the drawing my intention is to close the doc and make the drawing the active document. Right now I can close the part document but I dont know how to make the drawing active afterwards. It is going back to the assembly as active doc. Below is the code for the buttom, knowing that the OpenSelectedPart function opens the part/subassembly document from the item selected item from the list box.

               

              Sub CreateDrawing()

               

               

              Dim drawTemplate As String

               

               

              OpenSelectedPart True

               

               

              If swSelPart.GetPathName <> "" Then

               

               

                  drawTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)

                  If drawTemplate = "" Then

                      MsgBox "Template is not found"

                      Exit Sub

                  End If

                    

                  Set swDraw = swApp.NewDocument(drawTemplate, swDwgPaperSizes_e.swDwgPaperBsize, 0, 0)

                  swDraw.Create3rdAngleViews2 swSelPart.GetPathName()

              Else

                  MsgBox "Please save the model"

              End If

              'Debug.Print swSelPart.Name

              'Debug.Print swSelPart.Name2

               

               

              swApp.CloseDoc sCompFileName

               

               

              'ACTIVATE DRAWING HERE'

                 

              'Set swApp.ActiveDoc = swDraw

              End Sub

                • Re: OpenDoc6 error
                  Tapani Sjoman

                  Note: Edited code: sep, 11th

                   

                  You need to save drawing first and then hide model.

                   

                  f.e. something like this:

                   

                  Sub CreateDrawing()
                  
                  Dim swModel2        As ModelDoc2
                  Dim swModel3        As ModelDoc2
                  Dim drawTemplate    As String
                  Dim drawPathName    As String
                  Dim Errors          As Long
                  OpenSelectedPart True
                  Set swModel2 = swApp.ActiveDoc
                  drawPathName = Left(swModel2.GetPathName, Len(swModel2.GetPathName) - 6) & "SLDDRW"
                  If swModel2.GetPathName <> "" Then
                      drawTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)
                      If drawTemplate = "" Then
                          MsgBox "Template is not found"
                          Exit Sub
                      End If
                      Set swDraw = swApp.NewDocument(drawTemplate, swDwgPaperSizes_e.swDwgPaperBsize, 0, 0)
                      swDraw.Create3rdAngleViews2 swModel2.GetPathName()
                      Set swModel3 = swApp.ActiveDoc
                      swModel3.SaveAs3 drawPathName, 0, 0
                  Else
                      MsgBox "Please save the model"
                  End If
                  swModel2.Visible = False
                  End Sub