Export Idw to DXF

Export Idw to DXF

meck
Collaborator Collaborator
9,492 Views
25 Replies
Message 1 of 26

Export Idw to DXF

meck
Collaborator
Collaborator

Hi All,

I've been trying to get the Inventor help sample code PublishDXF to work, but I keep getting an error on line "Call DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)" The error message in attached. I'm running the code in an .ipt file using iLogic. I unfold a sheet metal part then place the flat pattern on an .idw with some text. I set the .idw active and then call the PublishDXF sub.

 

It may help if I knew what all the pieces of the code was doing. For instance what is the purpose of creating.ini file,  if that's what it is doing? Does it have to be placed in a certain location? It's just very difficult to debug when I do not know what any of the arguments are doing.

 

I've tried running the code in VBA from inside the idw, but I get the same error.

 

Please can someone give me some direction?

 

Thanks in advance.

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Accepted solutions (1)
9,493 Views
25 Replies
Replies (25)
Message 21 of 26

CattabianiI
Collaborator
Collaborator

Hi @meck,

 

I've completely got your problem finally.

If you (or anyone who will come here) want to go with the DataIO API and look for a faster way to add text to dxf file, try the code below.

 

SyntaxEditor Code Snippet

Public Sub EditDXFFile(TextToAdd As String)
    Dim realTextToAdd = TextToAdd + vbNewLine + " 0" + vbNewLine
    Dim textLen As Integer = realTextToAdd.Length()
    
    FileName = ThisDoc.FileName(False)
    oPath = ThisDoc.Path
    oRevNum = iProperties.Value("Project", "Revision Number")
    If oRevNum = "0" Or oRevNum = "" Then oRevNum = "1"
    sFname = oPath & "\" & FileName & "_" & oRevNum & ".dxf"   
    Dim readText As String = System.IO.File.ReadAllText(sFname)
    
    Dim re As New System.Text.RegularExpressions.Regex("(?<=ENTITIES((?!ENDSEC).)*)ENDSEC", System.Text.RegularExpressions.RegexOptions.Singleline)
    
    Dim i As Integer = 0
    For Each match In re.Matches(readText)        
        readText = readText.Insert(match.Index + textLen * i, realTextToAdd)      
        i = i + 1
    Next   
    System.IO.File.WriteAllText(sFname, readText)
End Sub

 

Regex explanation:
Match "ENDSEC" preceded by ( "ENTITIES" and ((any character not followed by the word "ENDSEC")  repeated zero or more times) )

The Singleline option tells regex engine to treat the input as if it consists of a single line; in other words the "."  matches every character including the newline.

 

I have to notice that with your code editing a 5000 lines dxf is as quick as mine.
But with 150000 lines dxf your code takes around a minute and with mine is instant.

 

Message 22 of 26

meck
Collaborator
Collaborator

To All,

I'm really sorry for bringing this thread to life again, but I have production processing complaining about the positioning of the flat pattern within the DXF.

I ended up using the DataIO way of creating the DXF file. It work very well and is extremely fast. Huge thank you to all who helped me with this project!

I know I have asked this question before, but did not get a useable answer. So at the risk of being chastised by Inventorland, I'm posting my entire code (see attached) for creating the DXF and begging for some kind of solution to my problem.

I really need a way to place the bottom left hand corner of the flat pattern at the (0,0,0) coordinates of the DXF file. At this point I don't care how it gets done. I'm even open to reverting back to using an idw to create the DXF. (Please don't make me do that.) 🙂

 

Any ideas no matter how crazy is really appreciated!

Mike

 

 

 

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
Message 23 of 26

LukeDavenport
Collaborator
Collaborator

Hi Mike,

When I was doing a similar thing a while ago, I had to resort to iterating through all the entities in the DXF text file, obtaining their X and Y locations to get the minimum XY point, then repositioning the whole lot by the required amount. Its simple enough to get max and min points for straight lines, but it gets interesting for arcs (bulge factors, start angle, end angle, mathematical fun for the whole family) if you want a really robust solution. 

 

I'd be very interested if anyone's got a simpler method that's foolproof. Or maybe the out of the box 'reposition to 0,0' option works a bit better these days?

 

Good luck!

0 Likes
Message 24 of 26

meck
Collaborator
Collaborator

Thanks for the response Luke!

How long did the process take to change all of the entities points in the DXF file? I would think it would take forever with complex flat patterns.

 

I have also found a problem with my code that I cannot figure out how to fix.

 

If I have a pattern of say 80 rectangles which converted to dxf is 320 lines, the dxf fails to open. If I just remove the text that I am adding the file works fine. If I cut the number of rectangles down in the pattern to like 50, the dxf works fine. I can only assume that the number of entities in the dxf file when I edit it by adding the text is causing the problem.

 

Is there a limit to the number of lines in a txt file or something related to that?

 

The (0,0) position is an unfortunate problem but having the dxf fail completely now that's a big problem.

 

If anyone has a suggestion it would be greatly appreciated.

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Message 25 of 26

MechMachineMan
Advisor
Advisor

Make the DXFS in an idw, locate as desired, export using IDW Export code. Problem solved. Smiley Very Happy


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

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 26 of 26

LukeDavenport
Collaborator
Collaborator

Hi Mike,

Sorry for the delay in replying.

 

Regarding the performance of the DXF text file editing, I haven't noticed it adding any time at all to the processing of each flat pattern. I just checked the time (just using a status bar text while processing), and for a 56,000 line DXF file it took less than 0.1 seconds to add the text in, so I don't think there should be a problem with performance - its only editing of a text file so it should be lightning fast.

 

If you're getting corrupted DXF files due to 'injecting' the text lines into the DXF text file, then you're probably not handling the 'handle' of each entity in the DXF file correctly. Every time you add a new entity (like a text line, an arc, or a line), you need to give it a 'handle' (this is the next line after the ' 5' code line for each entity). which is a hexadecimal integer. This handle should be the next available integer (one higher than the previous highest handle in the DXF). You also need to edit the overall $HANDSEED line in the DXF file and increase it by 1 every time you add a new entity.

 

I haven't checked the code suggested by another contributor to this thread to see whether this is being done at the moment, or whether you're just adding a static number as a 'handle' for the new text entity. Its probably the latter, and if so this is almost certainly the cause of the corruption. 

 

If you want to put the effort in to learn DXF structure then this is a good reference.

 

http://images.autodesk.com/adsk/files/autocad_2012_pdf_dxf-reference_enu.pdf

 

Otherwise, exporting the IDW is an easier option for sure.

 

Hope this helps a bit.

Luke

 

0 Likes