AnsweredAssumed Answered

Get a drawing when selecting a part from an assembly

Question asked by Никита Никита on Aug 31, 2020

Hello!
I can get a part from an assembly.
Now I need to get a drawing attached to it from this part. And then save the drawing in PDF format.
Below is a part of the code.
Thank.

 

Sub Main()
Dim swApp As Object
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim OpenAt As String
Dim value As String
Dim row As Integer
Dim kesh_file As String
Dim excel_path As String
Set swApp = Application.SldWorks
Dim swDrawing As DrawingComponent

 

kesh_file = "C:\PP\DCProject\DCP\TestPart\KESH.txt"
Open kesh_file For Input As #1
   Input #1, excel_path
Close #1
Set swModel = swApp.ActiveDoc
Set swSelectionMgr = swModel.SelectionManager
Set swEntity = swSelectionMgr.GetSelectedObject6(1, -1)


If swEntity Is Nothing Then
   Dim Message As String
   Message = "Select Detail"
   swApp.SendMsgToUser Message
   Exit Sub
Else
   Set swComponent = swEntity.GetComponent

            '<<<Get a copy of the drawing here
   Set xlApp = New Excel.Application
End If

detail_name = swComponent.GetPathName
detail_name = Left(detail_name, InStr(detail_name, "SLDPRT") - 2)
detail_name = Right(detail_name, Len(detail_name) - InStrRev(detail_name, "\"))
swApp.SendMsgToUser detail_name
Set xlWB = xlApp.Workbooks.Open(excel_path)
row = 8 'Начальная позиция
Set xlSheet = xlWB.Worksheets(1)
With xlSheet
   While xlSheet.Cells(row, 1).value <> ""
      row = row + 1
      Wend
   xlSheet.Cells(row, 1).value = detail_name
End With
xlWB.Save
xlWB.Close
xlApp.Quit
Set xlApp = Nothing
If xlApp Is Nothing Then Else Set xlApp = Nothing
Dim sKillExcel As String
sKillExcel = "TASKKILL /F /IM EXCEL.EXE"
Shell sKillExcel, vbHide
Exit Sub
End Sub

Outcomes