Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Express DXF macro

Anonymous

Express DXF macro

Anonymous
Not applicable

Hello all,

 

Im fairly skilled with Inventor but a complete noobie to macros/VBA's. I have some programming knowledge and would like some guidance in making a macro to do the following:

 

1. "Save Copy As" of a flat pattern that will be open

2. Request the user to input BOM quantity and assembly quantity

3. Multiply the BOM quantity by the assembly quantity to get an integer "Z"

4. Save the flat pattern as XXXXX-XX-XX-Z

 

Before I start coding I'd just like to know if this is possible and any advice I can get.

 

Thanks,

Brendan

 

0 Likes
Reply
Accepted solutions (1)
1,432 Views
14 Replies
Replies (14)

pball
Advisor
Advisor
Accepted solution

This is definitely possible and not that difficult. Here is some code I put together from something similar I had. It will prompt for the two quantities and multipy them, if the flat pattern doesn't exist yet it will create it, and then it will save the dxf in the same folder as the part with "-(totalquantity) added to the file name. Let me know how it works for you.

 

 

Public Sub Export_DXF()
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveEditDocument
    If (oDoc.FullDocumentName <> oDoc.FullDocumentName) Then
        MsgBox "Can't export while editing a part in an assembly"
        Exit Sub
    ElseIf (oDoc.ComponentDefinition.Type <> kSheetMetalComponentDefinitionObject) Then
        MsgBox "Not a sheet metal part Sorry."
        Exit Sub
    End If
    
    Dim BomQty As Integer
    BomQty = InputBox(Prompt:="Enter Bom Quantity", Title:="Bom Quantity")
    
    Dim AsmQty As Integer
    AsmQty = InputBox(Prompt:="Enter Assembly Quantity", Title:="Assembly Quantity")
    
    Dim TotQty As Integer
    TotQty = BomQty * AsmQty
    
    'If flat pattern doesn't exist, create it and return to folded view
    If (oDoc.ComponentDefinition.FlatPattern Is Nothing) Then
        Dim oDef As ControlDefinition
        oDoc.ComponentDefinition.Unfold
        Set oDef = ThisApplication.CommandManager.ControlDefinitions.Item("PartSwitchRepresentationCmd")
        oDef.Execute
    End If

    ' Get the DataIO object.
    Dim oDataIO As DataIO
    Set oDataIO = oDoc.ComponentDefinition.DataIO

    ' Build the string that defines the format of the DXF file.
    Dim sOut As String
    sOut = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=Outer&InvisibleLayers=IV_Tangent;IV_Bend;IV_Bend_Down;IV_Bend_Up;IV_Arc_Centers"

    ' Create the DXF file.
    oDataIO.WriteDataToFile sOut, full_path(oDoc.FullFileName) & filename_noext(oDoc.FullFileName) & "-" & TotQty & ".dxf"
End Sub

Function filename_noext(spth As String)
    'Returns filename without extension from full path
    If (spth <> "") And (InStr(spth, ".")) And (InStrRev(spth, "\") < InStrRev(spth, ".")) Then
        filename_noext = Mid(spth, InStrRev(spth, "\") + 1, InStrRev(spth, ".") - InStrRev(spth, "\") - 1)
    ElseIf (spth <> "") And Not (InStr(spth, ".")) Then
        filename_noext = Mid(spth, InStrRev(spth, "\", Len(spth)) + 1, Len(spth))
    Else:
        filename_noext = ""
    End If
End Function

Function full_path(spth As String)
    'Returns full path minus filename, trailing slash is included
    If spth <> "" Then
        full_path = Left(spth, Len(spth) - (Len(spth) - InStrRev(spth, "\")))
    Else:
        full_path = ""
    End If
End Function

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Anonymous
Not applicable

THANK YOU SO MUCH!

That works beautifully.

One more question, where does the code describe where to save the dxf to?

I would like it to go into a sub-folder named "DXF" that is in the same folder as the part I am exporting.

 

Thanks again,

Brendan

0 Likes

pball
Advisor
Advisor

The code for setting the file path is the second to last line in the main sub. I modified the line below to save the file in a subfolder named "DXF".

 

An example of how the code for the file name works.

original ipt file: c:\inventor\part1.ipt

 

full_path(oDoc.FullFileName) returns c:\inventor\

filename_noext(oDoc.FullFileName) returns part1

 

So adding those with other parts yields

dxf file: c:\inventor\DXF\part1-6.dxf

 

 

 

    'This line saves the dxf.
    oDataIO.WriteDataToFile sOut, full_path(oDoc.FullFileName) & "DXF\" & filename_noext(oDoc.FullFileName) & "-" & TotQty & ".dxf"

Hope that explains it well.

 

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Anonymous
Not applicable

Yup that worked again.

Any idea how to get material thickness from the sheet metal defaults?

For example here I would want 0.1875 to appear.

 

I'm looking on other forums as well but no luck so far.

 

Thanks,

Brendan

0 Likes

Anonymous
Not applicable

Hello,

 

I have same requirement. But i have other problem. I created DXf file with Inventor, but my supplier said their machine does not reconigze my file. We use different symbel to mark bend space.

AEAHHMZ001-1-001 is a smple from supplier.  1-004-004-1 is from Inventor. I am not sure if i can change the configuration to make my DXf same as supplier's. Now i can see that the layer and bending symbel are different.

 

Please kindly take a look attached files, and give suggetion.

 

 

Regards,

 

Jason

 

 

pball wrote:

The code for setting the file path is the second to last line in the main sub. I modified the line below to save the file in a subfolder named "DXF".

 

An example of how the code for the file name works.

original ipt file: c:\inventor\part1.ipt

 

full_path(oDoc.FullFileName) returns c:\inventor\

filename_noext(oDoc.FullFileName) returns part1

 

So adding those with other parts yields

dxf file: c:\inventor\DXF\part1-6.dxf

 

 

 

    'This line saves the dxf.
    oDataIO.WriteDataToFile sOut, full_path(oDoc.FullFileName) & "DXF\" & filename_noext(oDoc.FullFileName) & "-" & TotQty & ".dxf"

Hope that explains it well.

 

 


 

0 Likes

pball
Advisor
Advisor
brendansullivan:
You can use the following code to get the sheet metal thickness. The first two lines are not needed if you it with the code above as it already has those lines.

Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveEditDocument

Dim oSheetMetalCompDef As SheetMetalComponentDefinition
Set oSheetMetalCompDef = oDoc.ComponentDefinition

Dim thick As Double
'Inventor returns the thickness in metric so it needs converted
thick = oSheetMetalCompDef.Thickness.Value / 2.54
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Anonymous
Not applicable

Thank you. I had that same code except I didnt know the value was returned as metric.

 

Finally the code is all done and it works perfectly. Thanks so much.

 

Brendan

Anonymous
Not applicable

Weird, my ACAD doesnt recognize your supplier's drawing but it can open your drawing fine. Could it be that they have a different version of autocad? I'm using 2010 to open both files.

0 Likes

Anonymous
Not applicable

I use AutoCAD2014. I saved the file as 2010dxf and attaced it again. Can you please try it again?

0 Likes

Anonymous
Not applicable

Jason,

 

sOut = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=Outer&InvisibleLayers=IV_Tangent;IV_Bend;IV_Bend_Down;IV_Bend_Up;IV_Arc_Centers"

This is the line that determines the naming of the layers, colour of the layers and version of the dxf. It seems like you want to name OuterProfileLayer and Interior Profile Layer to be = to "Part" and change their colours to cyan. I'm not sure what the AM_5 layer is as it does not contain any objects in my autoCAD. There is also the code for IV_BEND and IV_BEND_DOWN in the code above. Perhaps you want to rename those? 

 

0 Likes

Anonymous
Not applicable

I can change the layer and color. But I do not know how to change the bending symbel as Ellipse. Can we change the bending symbel?

0 Likes

Anonymous
Not applicable

Can someone show me how to change the color with this code?

 

Thanks.

0 Likes

Anonymous
Not applicable

sOut = "FLAT PATTERN DXF?AcadVersion=2013&OuterProfileLayerColor=0;255;255&OuterProfileLayer=Part&InteriorProfilesLayer=Part" '&TangentLayer=Part&TangentLayer=Part"

 

 

you can change the color by chaning the 0;255;255 to something else.

0 Likes

Anonymous
Not applicable

Perfect, thanks.  How about changing the save path to lets say f:\DFX?

0 Likes