Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a macro to automatically generate a drawing 1

Status
Not open for further replies.

nickjb

Mechanical
Jun 19, 2007
35
0
0
GB
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 :)

 
Replies continue below

Recommended for you

A good place to start might be to record a macro performing the operations you desire. Then edit the code to clean it up and do more stuff.

You can also check these couple sites... Great resources for SW macros:


-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist
 
I've not used the task scheduler before. I've just had a quick look at it and can't see how it would help in this instance. Can you elaborate, please?
 
Use the "create Drawings" option in the task Scheduler.

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
Eastern Region SWUGN Representative SW 2007 SP 2.0
 
CBL, That's the idea. I've got an assembly of say 5 parts. I want to generate a drawing (for exporting as a DXF) with 5 views. 1 top view of each part in a single 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.

 
Its SW 2007 Education version. Its got all the tools expcept the PDM. I've got the Task Schedular but I can't see how it could do this.

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
 
Its SW 2007 Education version. Its got all the tools expcept the PDM. I've got the Task Schedular but I can't see how it could do this.

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
 
Thanks Tobin. That got me heading in the right direction. The first draft is working now. Needs tidying up and finessing.



' ******************************************************************************
' 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


 
Hi Tobin , thanks for the macro. Can you edit the macro to work for part configuration. I'm try to create a macro that will create flat pattern export as dxf of each configuration in the sheet metal part.



Deepak Gupta
SW2009 SP3.0
SW2007 SP5.0
MathCAD 14.0
 
Howdy,
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
 
Thanks Tobin for the courtesy and I can understand the situation.

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:
Option Explicit

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
 
Howdy,

Well - I'm so glad I started that sentence with "I could be wrong, but," :). Thanks for bring me up to date on that CBL.

Deepak your code worked great for me when I added the "." before the "DXF", like ".DXF".

Thanks

Tobin Sparks
 
Status
Not open for further replies.
Back
Top