Need help with macro to create drawings and export as .dxf.

Need help with macro to create drawings and export as .dxf.

Anonymous
Not applicable
3,096 Views
12 Replies
Message 1 of 13

Need help with macro to create drawings and export as .dxf.

Anonymous
Not applicable

Hi, All,

 

I am looking for a macro program to do the following.

 

1. create an drawing for a part using specified template. The program will read a list of part names. do one drawing at a time. The parts are all sheet metal parts

2. create view for the created drawing, select flat pattern view

3.  'Save copy as' an dxf file to specified destination.

 

If you could share the code or point me to the right source, it would very appreciated.

 

0 Likes
Accepted solutions (2)
3,097 Views
12 Replies
Replies (12)
Message 2 of 13

machiel.veldkamp
Collaborator
Collaborator

I almost have this exact thing for myself at work. I can copy it for you on monday (if I remember)

 

It just makes a new drawing for each missing object and places it on rougly the center of the drawing.

 

If it's a sheetmetal it will also check if there's a flat pattern and create it when it's not there (yet.)

 

 

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

___________________________
0 Likes
Message 3 of 13

Anonymous
Not applicable

That sounds great I'll try to send a message to remind you.

0 Likes
Message 4 of 13

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hello, here I have prepared a code that could help you. Since I do not know where you will take the list, I assumed that one could select the parts directly from the assembly file, through a "Do While" loop.
So in the assembly file, execute this macro.

 

Sub NewDrawing()
' Set path of drawing template file
Dim templateFile As String
templateFile = "E:\Inventor\Plantillas\RIAL_A4.idw"

'-------------------------------------------------
                        '''Set collection occurrences
Dim comps As ObjectCollection
Set comps = ThisApplication.TransientObjects.CreateObjectCollection
Dim comp As ComponentOccurrence

Do While True
Set comp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select a component")
If comp Is Nothing Then Exit Do
    Call comps.Add(comp)
Loop

'---------------------------------------------
                            ''' Create new drawing from sheetmetal
' Create a new NameValueMap object
Dim oBaseViewOptions As NameValueMap
Set oBaseViewOptions = ThisApplication.TransientObjects.CreateNameValueMap
Call oBaseViewOptions.Add("SheetMetalFoldedModel", False)  'True = folded view _False = flat pattern view


' If there are selected components we can do something
For Each comp In comps
    On Error Resume Next
    Dim oDoc As Document
    Set oDoc = comp.Definition.Document ' Document from occurrence

    'Remove extension .ipt
    Dim oFilename As String
    oFilename = Left(oDoc.FullFileName, (InStrRev(oDoc.FullFileName, ".", -1, vbTextCompare) - 1))

    If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
        Dim oDef As SheetMetalComponentDefinition
        oDef = oDoc.ComponentDefinition
        If oDef.HasFlatPattern = True Then
            Dim oDrawingDoc As DrawingDocument
            Set oDrawingDoc = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, templateFile, True)
            Call oDrawingDoc.Activate
            Dim oSheet As Sheet
            Set oSheet = oDrawingDoc.Sheets.Item(1)
            Dim oPoint1 As Point2d
            Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(5, 5)

            Dim oView As DrawingView
            Set oView = oSheet.DrawingViews.AddBaseView(oDoc, oPoint1, 1, ViewOrientationTypeEnum.kDefaultViewOrientation, DrawingViewStyleEnum.kHiddenLineDrawingViewStyle, , , oBaseViewOptions)
            Call oDoc.ReleaseReference
            ' Save New Drawing Document
            Call oDrawingDoc.SaveAs(oFilename & ".idw", True)
            'Call oDrawingDoc.Close
            
            Call FlatPatternDXF(oDoc, oFilename)
            Call oDoc.Close(True)
            
        End If
    End If
Next

End Sub

Private Sub FlatPatternDXF(oDoc As Document, oFilename As String)

'config
    'Change values located here to change output.
    Dim sOut As String
    sOut = "FLAT PATTERN DXF?AcadVersion=2000" _
    + "&OuterProfileLayer=OUTER_PROF&OuterProfileLayerColor=0;0;0" _
    + "&InteriorProfilesLayer=INNER_PROFS&InteriorProfilesLayerColor=0;0;0" _
    + "&FeatureProfileLayer=FEATURE&FeatureProfileLayerColor=0;0;0" _
    + "&BendUpLayer=BEND_UP&BendUpLayerColor=0;255;0&BendUpLayerLineType=37634" _
    + "&BendDownLayer=BEND_DOWN&BendDownLayerColor=0;255;0&BendDownLayerLineType=37634" _
'/config

'Processing:
    Dim oDataIO As DataIO
    Set oDataIO = oDoc.ComponentDefinition.DataIO
    
    Dim oDXFfileNAME As String
    oDXFfileNAME = oFilename & ".dxf"
    
    Call oDataIO.WriteDataToFile(sOut, oDXFfileNAME)

End Sub

Then you select the parts that you want to create the drawing files, if they are metal sheet and have the "FlatPattern" they will create the drawings of these parts. In the drawing you will create the "FlatPattern" view, then save this file and export a dxf from the source part file. If you want to close this drawing file you could activate the deactivated line (the green code line 'oDrawingDoc.Close)
I have deactivated this line because usually one must customize the drawing a little by adding dimensions notes, etc.
Try to make the code legible, divide it up so that the collection of objects is understood and how to work with them, if you have a list you could touch up the code and add your personalized part.

I hope the content is useful. regards


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 5 of 13

Anonymous
Not applicable

I'm new to the whole Macros thing in Inventor. When I pasted your code into the I logic rule it said that "let and set assignments are no longer supported" I'm using Inventor Professional 2018 if that's the problem or maybe I put the code in the wrong place.  If you could get back to me asap that would be great.

0 Likes
Message 6 of 13

Sergio.D.Suárez
Mentor
Mentor

What happens is that you asked for a macro above, which works in the VBA environment.
For what you describe now you want an Ilogic, which is something else. The ilogic do not support so-called "set" for example. What do you need?
Ilogic or macro?


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 7 of 13

Anonymous
Not applicable

I want a macro but I'm brand new to macros in Inventor and don't really know what I'm doing. If you could give me a quick run down of what I need to do with the code above that would be great.

0 Likes
Message 8 of 13

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Look, if you need an urgent ilogic, try this code. It happens that where I am I cut the light of a server and momentarily I can not access my default templates, so I can not test the rules that I generate because I do not have any test model. Make some changes to the macro code to try to adapt it to ilogic, you may have errors because I did it temporarily. I hope it works. When I get home I will check it and arm it to make it work as ilogic. regards

 

Sub main()
' Set path of drawing template file
Dim templateFile As String = "E:\Inventor\Plantillas\RIAL_A4.idw"

'-------------------------------------------------
                        '''Set collection occurrences
Dim comps As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim comp As ComponentOccurrence

While True
	comp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select a component") 
		
	' If nothing gets selected then we're done	
	If IsNothing(comp) Then Exit While
	comps.Add(comp) 
End While

'---------------------------------------------
                            ''' Create new drawing from sheetmetal
' Create a new NameValueMap object
Dim oBaseViewOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Call oBaseViewOptions.Add("SheetMetalFoldedModel", False)  'True = folded view _False = flat pattern view


' If there are selected components we can do something
For Each comp In comps
    On Error Resume Next
    Dim oDoc As Document = comp.Definition.Document ' Document from occurrence

    'Remove extension .ipt
    Dim oFilename As String
    oFilename = Left(oDoc.FullFileName, (InStrRev(oDoc.FullFileName, ".", -1, vbTextCompare) - 1))

    If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
        Dim oDef As SheetMetalComponentDefinition
        oDef = oDoc.ComponentDefinition
        If oDef.HasFlatPattern = True Then
            Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, templateFile, True)
            oDrawingDoc.Activate
            Dim oSheet As Sheet = oDrawingDoc.Sheets.Item(1)
            Dim oPoint1 As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(5, 5)

            Dim oView As DrawingView
            oView = oSheet.DrawingViews.AddBaseView(oDoc, oPoint1, 1, ViewOrientationTypeEnum.kDefaultViewOrientation, DrawingViewStyleEnum.kHiddenLineDrawingViewStyle, , , oBaseViewOptions)
            oDoc.ReleaseReference
            ' Save New Drawing Document
            oDrawingDoc.SaveAs(oFilename & ".idw", True)
            'oDrawingDoc.Close
            
            Call FlatPatternDXF(oDoc, oFilename)
            oDoc.Close(True)
            
        End If
    End If
Next

End Sub

Sub FlatPatternDXF(oDoc As Document, oFilename As String)

'config
    'Change values located here to change output.
    Dim sOut As String
    sOut = "FLAT PATTERN DXF?AcadVersion=2000" _
    + "&OuterProfileLayer=OUTER_PROF&OuterProfileLayerColor=0;0;0" _
    + "&InteriorProfilesLayer=INNER_PROFS&InteriorProfilesLayerColor=0;0;0" _
    + "&FeatureProfileLayer=FEATURE&FeatureProfileLayerColor=0;0;0" _
    + "&BendUpLayer=BEND_UP&BendUpLayerColor=0;255;0&BendUpLayerLineType=37634" _
    + "&BendDownLayer=BEND_DOWN&BendDownLayerColor=0;255;0&BendDownLayerLineType=37634" _
'/config

'Processing:
    Dim oDataIO As DataIO = oDoc.ComponentDefinition.DataIO
    
    Dim oDXFfileNAME As String
    oDXFfileNAME = oFilename & ".dxf"
    
    Call oDataIO.WriteDataToFile(sOut, oDXFfileNAME)

End Sub

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 9 of 13

Anonymous
Not applicable

I actually found a different rule that works great because it looks through an assembly for sheet metal parts.  The only thing I need now is to change the export options if that is at all possible and I made a separate post about that. Thanks for your help though.

https://forums.autodesk.com/t5/inventor-customization/change-dxf-export-options-in-macro/m-p/8856175...

0 Likes
Message 10 of 13

Sergio.D.Suárez
Mentor
Mentor

Here I found an assembly file to do a test. It's small but for the example it works.
In the video I show you how to load that macro code to execute it later.
You must put the path of your drawing template well written, otherwise you will not create the drawing from there.
You can change the coordinates of the insertion point, you could do it with formulas but I did not want to complicate the structure of the code too much.
Look at the end you created the dxf files, and the idw drawings. To name these files you have taken the same name from the sheet file and removed its extension. Then add the extension that corresponds to each file.


I hope I have been clear and the rule may be useful. regards


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 11 of 13

Anonymous
Not applicable

Sorry for going back and forth like this but I did a little more reading and realized that what I want is a macro. Is there any way you could edit the rule on the forum I linked to work as a macro or make this one search for all the sheet metal parts in the assembly like the rule does?  Thanks for the video as well I was able to use the macro provided above just fine.

0 Likes
Message 12 of 13

Sergio.D.Suárez
Mentor
Mentor

the macro and the ilogic that preprare do the same functions. To remove doubts, I ask, has any answer that I proposed to resolve this thread of the forum? To know if one should continue to try answers that can reach a solution. regards


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 13 of 13

Anonymous
Not applicable

I copied above program and pasted in my VB and tried to run the same but isnt working for some reason.

Need your suggestions here. Program doesnt show the error msg either

0 Likes