AnsweredAssumed Answered

Can someone make this macro great again please?

Question asked by Filip Efler on Jan 29, 2020
Latest reply on Feb 4, 2020 by Filip Efler

Hello community.

I have tryed to change this macro from vsta1 to vsta3 but without success. Macro did not work sometimes in SOLIDWORKS 2019 sp5 and i thought this change will help. What does this macro do?

It saves current drawing (mostly new one without name or file on disk) with my custom properties and creates a file in a same folder where assembly or part is.

It uses drawingnumber, position and name for ASSEMBLY. This is final a format of file DW100-C1-Testassembly.slddrw

and drawingnumber and name for PART. This is a final format of file DW200-testpart.slddrw 

Here is a code to look at and test files in the attachment. Good luck and thanks for your time

 

Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System

Partial Class SolidWorksMacro

Public Sub main()

Dim activeDoc As ModelDoc2 = swApp.ActiveDoc
Dim drawingDoc As DrawingDoc = Nothing

If (swDraw Is Nothing) Or (swDraw.GetType <> swDocDRAWING) Then

swApp.SendMsgToUser("To be used for drawings only, Open a drawing first and then TRY AGAIN!")
Exit Sub
Else
drawingDoc = activeDoc
End If

Dim SheetView As View = drawingDoc.GetFirstView() 'First view is always the sheet itself
Dim view As View = SheetView.GetNextView 'Ge the first "real" view

Dim model As ModelDoc2 = view.ReferencedDocument 'Get the referenced document
Dim confManager As ConfigurationManager = model.ConfigurationManager
Dim activeConfiguration As Configuration = confManager.ActiveConfiguration 'Load the active configuration

Dim propMgr As CustomPropertyManager = model.Extension.CustomPropertyManager("") 'activeConfiguration.Name

Dim name As String
Dim drawingNumber As String
Dim position As String
Dim concatenatedName As String
propMgr.Get5("nazev", False, Nothing, name, Nothing)
propMgr.Get5("cisloVykresu", False, Nothing, drawingNumber, Nothing)
propMgr.Get5("pozice", False, Nothing, position, Nothing)

If ((TypeOf model Is AssemblyDoc) And (position.Contains("C"))) Then
concatenatedName = drawingNumber + "-" + position + "-" + name
Else
concatenatedName = drawingNumber + "-" + name
End If

Dim errors As Integer
Dim warnings As Integer

Dim dirPath As String = My.Computer.FileSystem.GetParentPath(model.GetPathName()) + "\"
Dim fullPath As String = dirPath + concatenatedName + ".slddrw"
fullPath = fullPath.Replace("/", "")

Dim saveSuccessfull As Boolean = activeDoc.Extension.SaveAs(fullPath, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_UpdateInactiveViews, Nothing, errors, warnings)

If (Not saveSuccessfull) Then
MsgBox("Uložení se nezdaøilo. Èíslo chyby: " + errors.ToString, MsgBoxStyle.Critical, "Save drawing")
Else
MsgBox("Save was succesful under name::" + vbCrLf + vbCrLf + fullPath, MsgBoxStyle.Information, "Save drawing")
End If

End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub

''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks

Public Sub New()

End Sub
End Class

Outcomes