21 Replies Latest reply on Oct 5, 2017 11:01 AM by Jacob Corder

    get component name from drawing view

    Steve Anderson

      Hello:

       

      I would like to get the model reference (name and object) from the drawing view.  For some reason, this is not obvious.

       

      Thank you,

       

       

      Steve Anderson

          • Re: get component name from drawing view
            Steve Anderson

            Deepak:

             

            Here is my code.  Something is missing....,   I have already defined the smView variable...

             

                Private Sub ScaleDrawing()

                    FileExtension = Path.GetExtension(txtFile.Text)

                    Dim maxval As Object

                    swDrawing = swModel

             

                    Select Case FileExtension.ToLower

                        Case ".slddrw"

                            ' Get first View

                            Dim swView As View = GetFirstView()

                            Dim ViewName As String = swView.Name

             

                            ' Read Component

                            swDrawing.ViewZoomtofit2() ' Zooms drawing to fit, must be required for the selection

             

                            Dim swSelMgr As SelectionMgr = swDrawing.SelectionManager

                            ' swView = swSelMgr.GetSelectedObject6(1, -1) ' I already have an swView Defined as the first View in the drawing

                            MessageBox.Show(swView.Name) ' Drawing View5

             

                            Dim swDrawModel As ModelDoc2 = swView.ReferencedDocument

                            Dim sModelName As String = swView.GetReferencedModelName

                            MessageBox.Show(sModelName) ' Nothing!

                            MessageBox.Show(swDrawModel.GetPathName) ' Object not set to an instance of an object

             

                    End Select

             

                End Sub

             

                Private Function GetFirstView() As View

                    Dim swView As View

                    Dim swView2 As View

             

                    swView = swDrawing.GetFirstView     ' Sheet

                    swView2 = swView.GetNextView        ' First View on Sheet

             

                    Return swView2

             

                End Function

              • Re: get component name from drawing view
                Deepak Gupta

                Your code snippets looks fine to me. Could you please share the full macro to debug Or try following VBA codes.

                 

                Option Explicit

                Sub main()

                    Dim swApp                       As SldWorks.SldWorks

                    Dim swModel                     As SldWorks.DrawingDoc

                    Dim swView                      As SldWorks.View    

                 

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                    Set swView = swModel.GetFirstView

                    Set swView = swView.GetNextView   

                    Debug.Print "    Model  = " & swView.GetReferencedModelName

                    Debug.Print "      " & swView.ReferencedDocument.GetPathName

                End Sub

                  • Re: get component name from drawing view
                    Steve Anderson

                    Here's the complete code...

                     

                    Imports System.IO

                    Imports SolidWorks.Interop.sldworks

                    Imports SolidWorks.Interop.swconst

                     

                     

                    Public Class Form1

                        Public swApp As SldWorks

                        Public swModel As ModelDoc2

                        Public swDrawing As ModelDoc2

                        Public FileExtension As String

                     

                        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

                            ' Look for open file to assign default path and file variables to

                            Dim ModelFileDirectory As String = GetModelPath()

                     

                            ' Set Default Form Values

                            txtFile.Text = ModelFileDirectory

                            cboDrawingScaleRounding.SelectedIndex = 3   ' 4th items on the list

                            cboDetailScaleRounding.SelectedIndex = 2    ' 3rd item on the list

                              

                        End Sub

                     

                     

                        Private Sub cmdGetModel_Click(sender As Object, e As EventArgs) Handles cmdGetModel.Click

                            Dim OpenFileDialog1 As New OpenFileDialog

                            OpenFileDialog1.InitialDirectory = "C:\"

                            OpenFileDialog1.Filter = "SolidWorks Files|*.sldprt;*.sldasm;*.slddrw"

                            OpenFileDialog1.FilterIndex = 2

                            OpenFileDialog1.RestoreDirectory = True

                            If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

                                Dim FilePath As String = OpenFileDialog1.FileName

                                FileExtension = Path.GetExtension(FilePath)

                                Select Case FileExtension.ToLower

                                    Case ".sldprt"

                                        swModel = swApp.OpenDoc6(FilePath, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)

                     

                                    Case ".sldasm"

                                        swModel = swApp.OpenDoc6(FilePath, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)

                     

                                    Case ".slddrw"

                                        swModel = swApp.OpenDoc6(FilePath, swDocumentTypes_e.swDocDRAWING, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)

                     

                                End Select

                     

                                 txtFile.Text = FilePath

                     

                             End If

                     

                        End Sub

                     

                     

                        Private Sub ScaleDrawing()

                            FileExtension = Path.GetExtension(txtFile.Text)

                            Dim maxval As Object

                            swDrawing = swModel

                     

                            Select Case FileExtension.ToLower

                                Case ".slddrw"

                                    ' Get first View

                                    Dim swView As View = GetFirstView()

                                    Dim ViewName As String = swView.Name

                     

                                    ' Read Component

                                    swDrawing.ViewZoomtofit2() ' Zooms drawing to fit, must be required for the selection

                     

                                    Dim swSelMgr As SelectionMgr = swDrawing.SelectionManager

                                    ' swView = swSelMgr.GetSelectedObject6(1, -1) ' I already have an swView Defined as the first View in the drawing

                                    MessageBox.Show(swView.Name) ' Drawing View5

                     

                                    Dim swDrawModel As ModelDoc2 = swView.ReferencedDocument

                                    Dim sModelName As String = swView.GetReferencedModelName

                                    MessageBox.Show(sModelName) ' Nothing!

                                    MessageBox.Show(swDrawModel.GetPathName) ' Object not set to an instance of an object

                     

                       

                            End Select

                       

                     

                            'Select Case FileExtension.ToLower

                            '    Case ".sldprt"

                            '        maxval = ReadPart() * 1000

                     

                     

                            '    Case ".sldasm"

                            '        maxval = ReadAssembly() * 1000

                     

                     

                            'End Select

                     

                     

                        End Sub

                        Private Function GetFirstView() As View

                            Dim swView As View

                            Dim swView2 As View

                     

                            swView = swDrawing.GetFirstView     ' Sheet

                            swView2 = swView.GetNextView        ' First View on Sheet

                     

                            Return swView2

                     

                     

                        End Function

                      • Re: get component name from drawing view
                        Josh Brady

                        You declared swDrawing as ModelDoc2 instead of DrawingDoc

                          • Re: get component name from drawing view
                            Steve Anderson

                            Josh:

                            If I change this... from swDrawing = swModel, I see no difference in behavior.  I see these used interchangably in examples all the time, so I was not of the belief that it mattered. 

                            Decleration:

                                Public swDrawing As DrawingDoc

                                    swDrawing = swApp.ActiveDoc

                             

                            Full Sub Update:

                                Private Sub ScaleDrawing()

                                    FileExtension = Path.GetExtension(txtFile.Text)

                                    Dim maxval As Object

                             

                                    swDrawing = swApp.ActiveDoc

                             

                                    Select Case FileExtension.ToLower

                                        Case ".slddrw"

                                            ' Get first View

                                            Dim swView As View = GetFirstView()

                                            Dim ViewName As String = swView.Name

                             

                                            ' Read Component

                                            swDrawing.ViewZoomtofit2() ' Zooms drawing to fit, must be required for the selection

                             

                                            Dim swSelMgr As SelectionMgr = swDrawing.SelectionManager

                                            ' swView = swSelMgr.GetSelectedObject6(1, -1) ' I already have an swView Defined as the first View in the drawing

                                            MessageBox.Show(swView.Name) ' Drawing View5

                             

                                            Dim swDrawModel As ModelDoc2 = swView.ReferencedDocument

                                            Dim sModelName As String = swView.GetReferencedModelName

                                            MessageBox.Show(sModelName) ' Nothing!

                                            MessageBox.Show(swDrawModel.GetPathName) ' Object not set to an instance of an object

                             

                                    End Select

                            • Re: get component name from drawing view
                              Steve Anderson

                              Josh:

                              It would appear that this is where the rummer meets toe road, and it is not happening.

                               

                                              Dim swDrawModel As ModelDoc2 = swView.ReferencedDocument

                                              Dim sModelName As String = swView.GetReferencedModelName

                                              MessageBox.Show(sModelName) ' Nothing!

                                              MessageBox.Show(swDrawModel.GetPathName) ' Object not set to an instance of an object

                            • Re: get component name from drawing view
                              Steve Anderson

                              Deepak:

                               

                              It would appear that this is where the rummer meets toe road, and it is not happening.

                               

                                              Dim swDrawModel As ModelDoc2 = swView.ReferencedDocument

                                              Dim sModelName As String = swView.GetReferencedModelName

                                              MessageBox.Show(sModelName) ' Nothing!

                                              MessageBox.Show(swDrawModel.GetPathName) ' Object not set to an instance of an object

                          • Re: get component name from drawing view
                            Josh Brady

                            Unless you have cut a bunch of code out... Your sub "GetFirstView()" can't see the drawing swDrawing because it's not in scope.  You've declared it inside ScaleDrawing rather than at module level.

                              • Re: get component name from drawing view
                                Steve Anderson

                                Note:  swDrawing is Public.

                                 

                                Here's the complete code...

                                 

                                Imports System.IO

                                Imports SolidWorks.Interop.sldworks

                                Imports SolidWorks.Interop.swconst

                                 

                                Public Class Form1

                                    Public swApp As SldWorks

                                    Public swModel As ModelDoc2

                                    Public swDrawing As ModelDoc2

                                    Public FileExtension As String

                                 

                                    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

                                        ' Look for open file to assign default path and file variables to

                                        Dim ModelFileDirectory As String = GetModelPath()

                                 

                                        ' Set Default Form Values

                                        txtFile.Text = ModelFileDirectory

                                        cboDrawingScaleRounding.SelectedIndex = 3   ' 4th items on the list

                                        cboDetailScaleRounding.SelectedIndex = 2    ' 3rd item on the list

                                 

                                    End Sub

                                 

                                 

                                    Private Sub cmdGetModel_Click(sender As Object, e As EventArgs) Handles cmdGetModel.Click

                                        Dim OpenFileDialog1 As New OpenFileDialog

                                        OpenFileDialog1.InitialDirectory = "C:\"

                                        OpenFileDialog1.Filter = "SolidWorks Files|*.sldprt;*.sldasm;*.slddrw"

                                        OpenFileDialog1.FilterIndex = 2

                                        OpenFileDialog1.RestoreDirectory = True

                                        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

                                            Dim FilePath As String = OpenFileDialog1.FileName

                                            FileExtension = Path.GetExtension(FilePath)

                                            Select Case FileExtension.ToLower

                                                Case ".sldprt"

                                                    swModel = swApp.OpenDoc6(FilePath, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)

                                 

                                                Case ".sldasm"

                                                    swModel = swApp.OpenDoc6(FilePath, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)

                                 

                                                Case ".slddrw"

                                                    swModel = swApp.OpenDoc6(FilePath, swDocumentTypes_e.swDocDRAWING, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)

                                 

                                            End Select

                                 

                                            txtFile.Text = FilePath

                                 

                                        End If

                                 

                                    End Sub

                                 

                                 

                                    Private Sub ScaleDrawing()

                                        FileExtension = Path.GetExtension(txtFile.Text)

                                        Dim maxval As Object

                                 

                                        swDrawing = swModel

                                 

                                        Select Case FileExtension.ToLower

                                            Case ".slddrw"

                                                ' Get first View

                                                Dim swView As View = GetFirstView()

                                                Dim ViewName As String = swView.Name

                                 

                                                ' Read Component

                                                swDrawing.ViewZoomtofit2() ' Zooms drawing to fit, must be required for the selection

                                 

                                                Dim swSelMgr As SelectionMgr = swDrawing.SelectionManager

                                                ' swView = swSelMgr.GetSelectedObject6(1, -1) ' I already have an swView Defined as the first View in the drawing

                                                MessageBox.Show(swView.Name) ' Drawing View5

                                 

                                                Dim swDrawModel As ModelDoc2 = swView.ReferencedDocument

                                                Dim sModelName As String = swView.GetReferencedModelName

                                                MessageBox.Show(sModelName) ' Nothing!

                                                MessageBox.Show(swDrawModel.GetPathName) ' Object not set to an instance of an object

                                 

                                        End Select

                                 

                                        'Select Case FileExtension.ToLower

                                        '    Case ".sldprt"

                                        '        maxval = ReadPart() * 1000

                                 

                                 

                                        '    Case ".sldasm"

                                        '        maxval = ReadAssembly() * 1000

                                 

                                        'End Select

                                 

                                    End Sub

                                 

                                    Private Function GetFirstView() As View

                                        Dim swView As View

                                        Dim swView2 As View

                                 

                                        swView = swDrawing.GetFirstView     ' Sheet

                                        swView2 = swView.GetNextView        ' First View on Sheet

                                 

                                        Return swView2

                                 

                                    End Function

                                  • Re: get component name from drawing view
                                    Jacob Corder

                                    if it is a section view then this will help

                                     

                                    if swview.GetReferencedDocument is nothing then

                                         do until not  swview.getreferenceddocument is nothing

                                              swview = swview.getbaseview

                                         loop

                                    end if

                                     

                                     

                                    2016 SOLIDWORKS API Help - Get Base Views Example (VBA)

                                      • Re: get component name from drawing view
                                        Steve Anderson

                                        Jacob:

                                         

                                        In this case, the program is selecting the correct view, Drawing View5.  Its the first view after the sheet. I just now need to be able to access the model in the view to proceed.

                                         

                                        Why are these simple things always so difficult, as I'm still waiting to get to the rocket science part of this program!?

                                         

                                        Thanks,

                                         

                                         

                                        Steve Anderson

                                          • Re: get component name from drawing view
                                            Jacob Corder

                                            open the file in the view

                                            then try it to see if there is any difference.

                                             

                                            upload the files please

                                              • Re: get component name from drawing view
                                                Steve Anderson

                                                Jacob:

                                                 

                                                I have attached the project.

                                                 

                                                Thank you,

                                                 

                                                 

                                                Steve Anderson

                                                  • Re: get component name from drawing view
                                                    Jacob Corder

                                                    send the model your testing it on and the drawing.

                                                     

                                                    i am ready to test it

                                                      • Re: get component name from drawing view
                                                        Steve Anderson

                                                        Jacob:

                                                         

                                                        I recreated two parts, an assembly and two drawings (one for one of the parts and one for the assembly).

                                                        The program works great with these parts, which suggests there is an issue with the other drawing. 

                                                        Unfortunately, I cannot attach the customers drawing on this forum.  I will have to figure out what is wrong with their drawing as part of this issue.

                                                        Thanks for the input on this, and I will need to check for nulls more as you suggested.

                                                        Let me know if you have any further thoughts.

                                                         

                                                        Thanks,

                                                         

                                                         

                                                        Steve Anderson

                                                          • Re: get component name from drawing view
                                                            Jacob Corder

                                                            here is your problem

                                                             

                                                            you are running solidworks in an invisible state possibly. your using OpenDoc6 and as silent

                                                             

                                                            i believe that the drawingView is not loaded

                                                             

                                                            when checking for model in a view make sure you are calling

                                                             

                                                            IView.IsModelLoaded

                                                             

                                                            so

                                                            if View2.IsModelLoaded = false then

                                                                 View2.LoadModel

                                                            end if

                                                             

                                                             

                                                            what is happening is solidworks isnt loading the views when not visible as views are drawn. 

                                                             

                                                            here are a few methods to change in your code

                                                              Private Sub RescaleWithGaps(View2 As View)

                                                                    ' Count break lines

                                                                    Dim Size As Long

                                                                    Dim breaklines As Integer = View2.GetBreakLineCount2(Size)

                                                                    MessageBox.Show("Drawing View: " & View2.Name & ": " & breaklines.ToString & " breaks.")

                                                             

                                                             

                                                                    ' Get Breaklines - This is where it gets a little gray!

                                                                    Dim inst As IView = View2  'CHANGED CODE

                                                                    Dim val As System.Object = inst.GetBreakLines()

                                                                    'For Each item In items

                                                                    '    ' is there some example code to do a for each break in view, get gap??  Looking for the distance removed from the model, so add them up.

                                                                    'Next

                                                                End Sub

                                                             

                                                            SUB Scale Drawing

                                                                 MessageBox.Show(swView.Name)

                                                               If swView.IsModelLoaded = False Then

                                                                                swView.LoadModel()

                                                                            End If

                                                                            Dim swDrawModel As ModelDoc2 = swView.ReferencedDocument

                                                                            Dim sModelName As String = swView.GetReferencedModelName

                                                                            If swDrawModel Is Nothing Then

                                                                                If swModel.Visible = False Then

                                                                                    swModel.Visible = True

                                                                                    swDrawModel = swView.ReferencedDocument

                                                                                    sModelName = swView.GetReferencedModelName

                                                                                End If

                                                                            End If

                                                                           If Not swDrawModel Is Nothing Then

                                                                                MessageBox.Show(sModelName) ' Nothing!

                                                                                MessageBox.Show(swDrawModel.GetPathName) ' Object not set to an instance of an object

                                                             

                                                             

                                                                            Else

                                                                                MessageBox.Show("No Model in View: " & swView.GetName2 & " View type: " & swView.Type)

                                                                            End If

                                                            • Re: get component name from drawing view
                                                              Jacob Corder

                                                              basically i could recreate it only a few times and not every time which tells me the views did not load their models.

                                                               

                                                              i would bet money that this is why

                                                      • Re: get component name from drawing view
                                                        Jacob Corder

                                                        also. you need to null check more.

                                                         

                                                        check to see if the swmodel is nothing or not

                                                         

                                                        if not swmodel is nothing then

                                                             do code here

                                                        else

                                                             debug.print("Model is not loaded")

                                                        end if