I have a own drawing template for sheet metal parts. So it is possible to put predefined flat pattern view into template?
Sorry it is still a limitation.
I am trying to do this as well. Has anyone figured this out? We have a ton of sheet metal parts I would love to drop into a multi sheet drawing template with standard views on page one and flat pattern view on page two.
Macro might be a way to do that but I've not tried/checked that.
One possible method (if your configuration name is always the same - more complicated if they are not) is to create a drawing template with a model in it that defines the views you want (including the flat pattern view). In the model that you want to create a new drawing for, create a derived configuration using the base name of the parent configuration appended with -flatSM-FLAT-PATTERN. Set this configuration to show the part in its flattened state. Save and close the model (you can leave it open but then you will need to answer a pop up message). Now browse to your "drawing template" in the file open dialog and select the drawing but do not open it. Click the reference button and change the model the drawing is looking at to be your new model. Now open the drawing.
I only tested this theory a couple of times and it worked well for a simple (1-2 bend) sheet metal part.
I have seen that you can add macros embedded into part files, but I will have to dig to see if it can be done in a drawing file.
Sorry you can not embed a macro in drawing but can make the macro to run when you start a new drawing.
I'm trying to put a macro into a template that will make one of the existing views in my template a flat pattern view (or insert a flat pattern view - with bend notes on).
I'm currently doing all of this manually and am stumped by
a) having the macro run automatically upon opening the drawing and
b) changing to or inserting a flat- pattern view.
I've ran my record action macro a couple of time and so far it has not picked up any of my activities.
Any thoughts on code I could review?
Neil, it can be done via Event Notifiers using FileNewNotify2EventHandler (check example hre: auto prompt save as when opening and to autorun the macro, check here: Is there any way to force start a macro on startup of solidworks)
Vote for SPR 487551: Impossibility to create a drawing template from where visualize in automatic way the part in its flatted status for a chance at seeing it in a future release.
How do I vote on an SPR in the Knowledge Base?
YES! Thank you, just voted - I love the verbiage too "Impossibility to create a drawing template from where visualize in automatic the part in its flatted status" ie - Just let me have a flat view in my template all ready!!!
That being said, I'm sure it's some involved programming to have a view tied to a configuration that does not exist in all parts.
Here's what I was able to hobble together for my flat check prints that I send to programming. I still need to add the autorun portion of this macro - my end goal is for it to execute whenever I use my flat check print template.
'**********************'This code is a patch work of a bunch of stuff I've found, I'll try to credit everyone's work that I've used. My apologies to anyone I've forgotten.'NJ
'What this Macro does is a) scans existing views in an unsaved drawing to determine what part is being referenced'then b) saves the part to a custom variable (in this case variable named "F") within that part. For the record I've set "F" equal to $PRP:"SW-File Name" - Flat'Now that the drawing is saved, we then insert a flat pattern of the part at a certain x,y within the drawing.'We then insert a bend table.'I'm hoping to develop this further to have the macro auto run upon opening of the template it is used in.'And then after that I'd like to be able to offset (both ways) construction lines equal to .5 of the bend deduction at each bend'. . . . . . . . all automatically' . . . . . . . . someday
' Thus far I've modified existing code from several sources, Artem Taturevych, Deepak Gupta are some but not all.
'This code is designed to be ran on sheet metal parts that flatten with out errors. I have no idea what happens if you run it on a non sheet metal part or assembly.
'**********************Option ExplicitPublic Enum NameSource_e DefaultDrawingViewFileName DefaultDrawingViewCustomProperty CustomPropertyEnd Enum
Const NAME_SOURCE As Integer = 1 'I'm not sure why but the code did not work until I set this to 1 it used to be set = to "NameSource_e.CustomProperty"Const PRP_NAME As String = "F" 'Here's we're telling the code which variable to name the file, originally I want to rename my file, the existing part name + a custom variable. I ended up only getting the custom variable to come thru so I cheated and baked the part name into the custom variable.
Const AUTO_SAVE As Boolean = True
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks Dim swModel As SldWorks.ModelDoc2 Set swModel = swApp.ActiveDoc'OK This part is the scanning and saving to custom variable for drawing name If Not swModel Is Nothing Then If swModel.GetPathName() = "" Then Dim newFileName As String 'Debug.Print "swModel = " & swModel Debug.Print "Auto_Save = " & AUTO_SAVE Debug.Print "NAME_SOURCE = " & NAME_SOURCE Debug.Print "PRP_NAME = " & PRP_NAME Debug.Print "newFileName(1) = " & newFileName
newFileName = CreateSaveFileName(swModel, Not AUTO_SAVE, NAME_SOURCE, PRP_NAME) Debug.Print "newFileName(2) = " & newFileName If newFileName <> "" Then If AUTO_SAVE Then Dim errs As Long Dim warns As Long 'I'm not sure what this does but am afraid to take it out. If False = swModel.Extension.SaveAs(newFileName, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, errs, warns) Then Err.Raise vbError, "", "Failed to save model: " & errs End If Else If False = swModel.SetTitle2(newFileName) Then Err.Raise vbError, "", "Failed to set model title" End If End If Else Err.Raise vbError, "", "Failed to generate file name" End If End If Else Err.Raise vbError, "", "Model is nothing" End If
'ok now that we've saved (and named) our drawing we're going to insert a flat pattern of the part.
Dim swDrawing As SldWorks.DrawingDocDim swSheet As SldWorks.SheetDim filename As StringDim Fview As View
Dim ConfigName As String
Dim HideBendLines As Boolean
Dim LocX As Double
Dim LocY As Double
Dim LocZ As Double
Dim FlipView As Boolean
Dim value As View
Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc Set swDrawing = swModel
filename = swModel.GetPathName filename = Left(filename, InStr(filename, ".") - 8) 'Here we are peeling off what we just added to the drawing name starting behind the ., in case we are removing " - Flat." filename = filename & ".sldprt" 'and taking that part name and turning it back into the part that - this is the part that we will be making a flat pattern of. Getting the flat pattern in is easy once the drawing is saved. ' Insert the Flat Pattern View
LocX = 1.125
LocY = 1.5
LocZ = 0
HideBendLines = False
ConfigName = "FLAT PATTERN" 'I tried several times to find out what the configuration name was, finally landed on this and it works!
Debug.Print "Filename = " & filenameDebug.Print "Configname = " & ConfigNameDebug.Print "X Location = " & LocXDebug.Print "Y Location = " & LocYDebug.Print "Z Location (Should be 0) = " & LocZDebug.Print "Hidden Bend Lines (False) = " & HideBendLines
Set Fview = swDrawing.CreateFlatPatternViewFromModelView3(filename, ConfigName, LocX, LocY, LocZ, HideBendLines, False) 'and now our flat view in, if you've adjusted the document's properties you will see the flat, possibly with bend notes. I've edited my bend note definition (You'll need to google this as its a little evolved) to include the Bend Deduction that defines the bend it's attached to. Very useful when double checking flats.
'Insert Bend table for the flat pattern
'Here I've inserted a bend table, this cleans up the flat pattern as the bend notes can be distracting
Dim myBendTableAnnot As BendTableAnnotation
Dim myBendTableFeat As BendTable
Dim checkname As String
Set myBendTableAnnot = Fview.InsertBendTable(False, 0.0525, 0.34, swBOMConfigurationAnchor_BottomLeft, "A", "N:\Solidworks Templates\english\bendtable+Bend Deduction.sldbndtbt") 'You of course will want to point this to your bend table - not mine, you also might not like where I've placed mine.
Set myBendTableFeat = myBendTableAnnot.BendTable
Debug.Print "Starting tag: " & myBendTableFeat.StartingValue
Debug.Print "swBendTableTagStyle_e option: " & myBendTableFeat.TagStyle
Debug.Print "Number of bend table annotations: " & myBendTableFeat.GetTableAnnotationCount
'End Sub 'Everything below is from the code i used to scan the pre-existing views (already in my template) to determine the part in those views and to save the drawing to a custom variable within the part. End Sub
Function CreateSaveFileName(model As SldWorks.ModelDoc2, nameOnly As Boolean, src As NameSource_e, Optional prpName As String = "") Dim swCurModel As SldWorks.ModelDoc2 Dim filename As String If src = NameSource_e.CustomProperty Then Dim swConf As SldWorks.Configuration Set swConf = model.ConfigurationManager.ActiveConfiguration Dim confName As String If Not swConf Is Nothing Then confName = swConf.Name Else confName = "" End If Set swCurModel = model filename = GetCustomPropertyValue(model, prpName, confName) ElseIf src = NameSource_e.DefaultDrawingViewFileName Or src = NameSource_e.DefaultDrawingViewCustomProperty Then If model.GetType() = swDocumentTypes_e.swDocDRAWING Then Dim swView As SldWorks.View Dim swDraw As SldWorks.DrawingDoc Set swDraw = model Set swView = GetDefaultView(swDraw.GetCurrentSheet()) If Not swView Is Nothing Then Dim swViewModel As SldWorks.ModelDoc2 Set swViewModel = swView.ReferencedDocument Set swCurModel = swViewModel If Not swViewModel Is Nothing Then If src = NameSource_e.DefaultDrawingViewFileName Then filename = GetFileName(swViewModel) ElseIf src = NameSource_e.DefaultDrawingViewCustomProperty Then filename = GetCustomPropertyValue(swViewModel, prpName, swView.ReferencedConfiguration) Else Err.Raise vbError, "", "Not supported source for drawing" End If Else Err.Raise vbError, "", "Failed to get model from view" End If Else Err.Raise vbError, "", "Failed to find default view" End If Else Err.Raise vbError, "", "Source is only applicable for drawings" End If Else Err.Raise vbError, "", "Not supported source" End If If nameOnly Then CreateSaveFileName = filename Else CreateSaveFileName = GetFilePath(model, swCurModel, filename) End If End Function
Function GetFileName(model As SldWorks.ModelDoc2) As String Dim path As String path = model.GetPathName Dim filename As String filename = Right(path, Len(path) - InStrRev(path, "\")) filename = Left(filename, InStrRev(filename, ".") - 1) GetFileName = filename End Function
Function GetFilePath(targModel As SldWorks.ModelDoc2, srcModel As SldWorks.ModelDoc2, filename As String) Dim ext As String Select Case targModel.GetType() Case swDocumentTypes_e.swDocPART ext = ".sldprt" Case swDocumentTypes_e.swDocASSEMBLY ext = ".sldasm" Case swDocumentTypes_e.swDocDRAWING ext = ".slddrw" End Select Dim dirPath As String dirPath = srcModel.GetPathName Debug.Print "dirPath = " & dirPath
If dirPath = "" Then
Err.Raise vbError, "", "Model is not saved" End If dirPath = Left(dirPath, InStrRev(dirPath, "\")) GetFilePath = dirPath & filename & ext End Function
Function GetCustomPropertyValue(model As SldWorks.ModelDoc2, prpName As String, confName As String) Dim swCustPrpMgr As SldWorks.CustomPropertyManager Set swCustPrpMgr = model.Extension.CustomPropertyManager(confName) Dim val As String Dim resVal As String swCustPrpMgr.Get4 prpName, False, val, resVal If resVal = "" Then Set swCustPrpMgr = model.Extension.CustomPropertyManager("") swCustPrpMgr.Get4 prpName, False, val, resVal End If GetCustomPropertyValue = resVal End Function
Function GetDefaultView(swSheet As SldWorks.Sheet) As SldWorks.View Dim vViews As Variant vViews = swSheet.GetViews If Not IsEmpty(vViews) Then Dim i As Integer For i = 0 To UBound(vViews) Dim swView As SldWorks.View Set swView = vViews(i) If UCase(swView.Name) = UCase(swSheet.CustomPropertyView) Then Set GetDefaultView = swView Exit Function End If Next Set GetDefaultView = vViews(0) 'use first one End If End Function
Retrieving data ...