Creating a macro to automatically generate a drawing
Creating a macro to automatically generate a drawing
(OP)
I am considering writing a macro for setting up 2D files for our CNC machine. What I want it to do is create a drawing of an assembly and add a 'top' view for every individual part within the assembly. I have a 'manufacture method' custom property so I might use that to weed out the parts that don't use 2D CNC but for now adding a view of every part would be a good start.
Before I begin:
Has someone done this before and is willing to share?
Is it going to be hard to write and more trouble than it is worth?
I've written a few macros but I'm pretty new to it so not sure where to begin. Any helpful hints and tips would be appreciated. Some suggested code would be even better :)
Before I begin:
Has someone done this before and is willing to share?
Is it going to be hard to write and more trouble than it is worth?
I've written a few macros but I'm pretty new to it so not sure where to begin. Any helpful hints and tips would be appreciated. Some suggested code would be even better :)






RE: Creating a macro to automatically generate a drawing
You can also check these couple sites... Great resources for SW macros:
http://www.lennyworks.com/solidworks/
http://sw.fcsuper.com/index.php
http://swtools.cad.de/us_prog_pac.htm
http://batchprocess.angelsix.com/
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist
RE: Creating a macro to automatically generate a drawing
Rob Rodriguez CSWP
www.axiscadsolutions.com
www.robrodriguez.com
http://www.robrodriguezblog.com
Eastern Region SWUGN Representative www.swugn.org www.nvtswug.com
SW 2007 SP 2.0
RE: Creating a macro to automatically generate a drawing
Matt Lorono
CAD Engineer/ECN Analyst
Silicon Valley, CA
Lorono's SolidWorks Resources
Co-moderator of Solidworks Yahoo! Group
and Mechnical.Engineering Yahoo! Group
RE: Creating a macro to automatically generate a drawing
RE: Creating a macro to automatically generate a drawing
If you set up a drawing template with predefined views you can tell the task Scheduler to use the template and which folder to pull model files from.
Task Scheduler will keep creating drawings until the model files run out.
You can set up your template with custom properties so they are automatically filled out at drawing creation and if you set the template settings correctly you can also aautomatically import model items and dimensions at drawign creation.
For simple parts it will work great. For more coplex parts i'd just stick with adding the drawing views, custom properies.
Rob Rodriguez CSWP
www.axiscadsolutions.com
www.robrodriguez.com
http://www.robrodriguezblog.com
Eastern Region SWUGN Representative www.swugn.org www.nvtswug.com
SW 2007 SP 2.0
RE: Creating a macro to automatically generate a drawing
RE: Creating a macro to automatically generate a drawing
Rob Rodriguez CSWP
www.axiscadsolutions.com
www.robrodriguez.com
http://www.robrodriguezblog.com
Eastern Region SWUGN Representative www.swugn.org www.nvtswug.com
SW 2007 SP 2.0
RE: Creating a macro to automatically generate a drawing
RE: Creating a macro to automatically generate a drawing
They don't need to auto nest as its easy to shuffle them around. Its the opening of each part to create the view that is time consuming. They are wood or plastic parts to be cut from sheet.
rockguy, I can't seem to find the feature in task scheduler that will allow me to do this. Can you point me in the right direction.
RE: Creating a macro to automatically generate a drawing
What version os SW do you have? If it is core SW you will not find Task Schedular. It is in the higher versions of SW.
Cheers,
Anna Wood
Anna Built Workstation, Core i7 EE965, FirePro V8700, 12 gigs of RAM, OCZ Vertex 120 Gig SSD
SW2009 SP3.0, Windows 7 RC1
http://www.solidmuse.com
http://www.phxswug.com
RE: Creating a macro to automatically generate a drawing
I've made a start with a Macro
I've managed to generate a list of parts in the assembly (with help from google and published code). Struggling a bit to create the drawing. I tried recording a macro but that flashed up loads of errors
' ************************************************************
' C:\DOCUME~1\NICKBA~1\LOCALS~1\Temp\swx240\Macro1.swb - macro recorded on 08/06/08 by Nick Banks
' ************************************************************
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim swView As String
Dim swFileName As String
Dim swFilePath As String
Dim swParts(0 To 100) As String
Dim boolstatus As Boolean
Dim Feature As Object
Dim Child As Object
Dim ModDoc As Object
Dim i As Integer
Dim j As Integer
Dim Children As Variant
Dim InfoText As String
Dim swModel As Object
Sub main()
Set swApp = Application.SldWorks
Set ModelDoc2 = swApp.ActiveDoc
Set Configuration = ModelDoc2.GetActiveConfiguration
Set Component2 = Configuration.GetRootComponent
Set ModDoc = Component2.GetModelDoc
Children = Component2.GetChildren()
ChildCount = UBound(Children) + 1
i = 0
Do While i <> ChildCount
Set Component2 = Children(i)
Set ModDoc = Component2.GetModelDoc
swFileName = Component2.Name2
swFilePath = Component2.GetPathName
InfoText = InfoText & "Item " & i & swFilePath & " End" & vbNewLine
swParts(i) = swFilePath
i = i + 1
Loop
MsgBox InfoText, vbOKOnly
Set Part = swApp.NewDocument("*.drt", 12, 0.2794, 0.4318)
Dim DrawView As Object
i = 0
Do While i <> ChildCount
swView = swParts(i)
MsgBox ("creating view: " & swView)
'Create view
i = i + 1
Loop
End Sub
RE: Creating a macro to automatically generate a drawing
I've made a start with a Macro
I've managed to generate a list of parts in the assembly (with help from google and published code). Struggling a bit to create the drawing. I tried recording a macro but that flashed up loads of errors
' ************************************************************
' C:\DOCUME~1\NICKBA~1\LOCALS~1\Temp\swx240\Macro1.swb - macro recorded on 08/06/08 by Nick Banks
' ************************************************************
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim swView As String
Dim swFileName As String
Dim swFilePath As String
Dim swParts(0 To 100) As String
Dim boolstatus As Boolean
Dim Feature As Object
Dim Child As Object
Dim ModDoc As Object
Dim i As Integer
Dim j As Integer
Dim Children As Variant
Dim InfoText As String
Dim swModel As Object
Sub main()
Set swApp = Application.SldWorks
Set ModelDoc2 = swApp.ActiveDoc
Set Configuration = ModelDoc2.GetActiveConfiguration
Set Component2 = Configuration.GetRootComponent
Set ModDoc = Component2.GetModelDoc
Children = Component2.GetChildren()
ChildCount = UBound(Children) + 1
i = 0
Do While i <> ChildCount
Set Component2 = Children(i)
Set ModDoc = Component2.GetModelDoc
swFileName = Component2.Name2
swFilePath = Component2.GetPathName
InfoText = InfoText & "Item " & i & swFilePath & " End" & vbNewLine
swParts(i) = swFilePath
i = i + 1
Loop
MsgBox InfoText, vbOKOnly
Set Part = swApp.NewDocument("*.drt", 12, 0.2794, 0.4318)
Dim DrawView As Object
i = 0
Do While i <> ChildCount
swView = swParts(i)
MsgBox ("creating view: " & swView)
'Create view
'How????????
i = i + 1
Loop
End Sub
RE: Creating a macro to automatically generate a drawing
Attached is a macro that might have some code you'd be interested in. This macro won't actually work for you unless you change it to point to your Template names and locations.
Hope this helps
Tobin Sparks
www.nov.com
RE: Creating a macro to automatically generate a drawing
' ******************************************************************************
' C:\DOCUME~1\NICKBA~1\LOCALS~1\Temp\swx240\Macro1.swb - macro recorded on 08/06/08 by Nick Banks
' ******************************************************************************
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim swView As String
Dim swType As String
Dim swFileName As String
Dim swFilePath As String
Dim swParts(0 To 100) As String
Dim boolstatus As Boolean
Dim Feature As Object
Dim Child As Object
Dim ModDoc As Object
Dim i As Integer
Dim j As Integer
Dim Children As Variant
Dim InfoText As String
Dim swModel As Object
Dim TopAssy As String
Dim longstatus As Long, longwarnings As Long
Dim PosX As Double, PosY As Double
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
TopAssy = swModel.GetPathName
swType = swModel.GetType
If swType = swDocASSEMBLY Then
Set ModelDoc2 = swApp.ActiveDoc
Set Configuration = ModelDoc2.GetActiveConfiguration
Set Component2 = Configuration.GetRootComponent
Set ModDoc = Component2.GetModelDoc
InfoText = ""
Children = Component2.GetChildren()
ChildCount = UBound(Children) + 1
i = 0
Do While i <> ChildCount
Set Component2 = Children(i)
Set ModDoc = Component2.GetModelDoc
swFileName = Component2.Name2
swFilePath = Component2.GetPathName
InfoText = InfoText & "Item " & i & swFilePath & " <" & vbNewLine
swParts(i) = swFilePath
i = i + 1
Loop
MsgBox InfoText, vbOKOnly
i = 0
Do While i <> ChildCount
swFilePath = swParts(i)
' MsgBox ("opening: " & swFilePath)
Set Part = swApp.OpenDoc6(swFilePath, 1, 0, "", longstatus, longwarnings)
swApp.ActiveDoc.ActiveView.FrameLeft = 0
swApp.ActiveDoc.ActiveView.FrameTop = 0
swApp.ActiveDoc.ActiveView.FrameState = 1
swApp.ActiveDoc.ActiveView.FrameState = 1
Set Part = swApp.ActivateDoc2(swFilePath, False, longstatus)
If Part Is Nothing Then
Call MsgBox("Unable to open document!", vbExclamation, "Line3") ' Display error message
End If
i = i + 1
Loop
Set Part = swApp.ActivateDoc2(TopAssy, False, longstatus)
sTemplateName = "\\srv-Lancelot\aardvark\CAD\Templates\Pacer CNC.slddrt"
Set swDrawing = swApp.NewDocument(sTemplateName, swDwgPaperAsize, 0#, 0#)
' display sheet format
Set swNewSheet = swDrawing.GetCurrentSheet
swNewSheet.SheetFormatVisible = True
swDrawing.EditSheet
i = 0
Do While i <> ChildCount
swView = swParts(i)
'MsgBox ("creating view: " & swView)
PosX = 0.3 + (0.2 * i)
PosY = 0.3 + (0.02 * i)
'Create view
DrawView = swDrawing.CreateDrawViewFromModelView(swView, "*Top", PosX, PosY, 0)
'set scale
boolstatus = swNewSheet.SetScale(1, 1, True, True)
i = i + 1
Loop
End If
If swType = swDocPART Then
MsgBox ("Its a part")
End If
End Sub
RE: Creating a macro to automatically generate a drawing
Deepak Gupta
SW2009 SP3.0
SW2007 SP5.0
MathCAD 14.0
RE: Creating a macro to automatically generate a drawing
I'm glad you like the macro. Unfortunately I'm not in a position to do much for others outside the company I work for. I'd be glad to share anything I may have done though.
I could be wrong, but, I'm pretty sure SolidWorks will only generate a *.dxf file from an existing *.slddrw document. I don't know of a way to go straight from *.sldprt to *.dxf, there would have to be a *.slddrw in between. If that's what your asking
Thanks
Tobin Sparks
www.nov.com
RE: Creating a macro to automatically generate a drawing
RE: Creating a macro to automatically generate a drawing
Thanks CBL for the info and I have been trying it.
I have been able to make the macro (by combining 2 macros) but getting issues with the file name. Being a beginer I'm not able to fix the file naming issue. The file name should be file name followed by config name.dxf but it comes as file name should be file name.config name dxf
Here are the codes if someone can fix
CODE
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vConfNameArr As Variant
Dim sConfigName As String
Dim nStart As Single
Dim i As Long
Dim bShowConfig As Boolean
Dim bRebuild As Boolean
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
vConfNameArr = swModel.GetConfigurationNames
For i = 0 To UBound(vConfNameArr)
sConfigName = vConfNameArr(i)
bShowConfig = swModel.ShowConfiguration2(sConfigName)
bRebuild = swModel.ForceRebuild3(False)
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
FilePath = swModel.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
' This line generate the file name with extension but it is not coming as expected
NewFilePath = PathNoExtension + sConfigName & "DXF"
'Export Flat Pattern
bRet = swModel.ExportFlatPatternView(NewFilePath, 1)
Next i
End Sub
Deepak Gupta
SW2009 SP3.0
SW2007 SP5.0
MathCAD 14.0
RE: Creating a macro to automatically generate a drawing
Well - I'm so glad I started that sentence with "I could be wrong, but,"
Deepak your code worked great for me when I added the "." before the "DXF", like ".DXF".
Thanks
Tobin Sparks
www.nov.com
RE: Creating a macro to automatically generate a drawing
Thanks for the quick updates. It wasn't working for me earlier but to be strange they working now.
Deepak Gupta
SW2009 SP3.0
SW2007 SP5.0
MathCAD 14.0