Code doesn't work anymore for dxf

Code doesn't work anymore for dxf

berry.lejeune
Advocate Advocate
154 Views
3 Replies
Message 1 of 4

Code doesn't work anymore for dxf

berry.lejeune
Advocate
Advocate

Hello all,

 

I've been using a code to make dxf's but since yesterday the code doens't work anymore

The workflow is as follow:

  • Select a face
  • Run the code
  • Code makes a subfolder in the folder where the part is in (folder called "dxf")
  • Code makes a dxf with the partnumber of the part
  • Code automatically opens the folder where the dxf is made in

What all of the sudden happened is: 

  • Select face
  • Run the code
  • Code makes a subfolder in the folder where the part is in (folder called "dxf")
  • Opens a "save copy as" and this is not in the correct folder
  • I have to enter the partnumber to save it and select the correct folder
  • After I press the "save as" button the dxf is made
  • Code automatically opens the folder where the dxf is made in

edit: I'm running 2025

Here's the code:

 

'Export face to pre-defined folder

If ThisApplication.ActiveDocument.SelectSet.Count = 0 Then
	MsgBox("Face not selected. Aborting Rule!")
	Exit Sub
End If

oPath = ThisDoc.Path
'get DXF target folder path
oFolder = oPath & "\" & "DXF"
'Check for the dxf folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If
oFileName = oFolder & "\" & ThisDoc.FileName & ".dxf"

Dim oCmdMgr As CommandManager
oCmdMgr = ThisApplication.CommandManager

Call oCmdMgr.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oFileName)
Call oCmdMgr.ControlDefinitions.Item("GeomToDXFCommand").Execute

'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)

 

0 Likes
155 Views
3 Replies
Replies (3)
Message 2 of 4

machiel.veldkamp
Collaborator
Collaborator

Which version of Inventor are you running. In 2024 this code works as you described

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

___________________________
0 Likes
Message 3 of 4

berry.lejeune
Advocate
Advocate

I'm running 2025. But before the holidays it was still working, and since I started again last week it doens't work anymore

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

There are a few variables involved which can potentially change from one run to the next, so I like to try to catch every possibility along the way, at least when it comes to my 'production use' automation solutions / tools.  Things like checking document type, checking if the document has been saved yet (if not, then no path or file name), checking if there are multiple pre-selections (if only expecting one), and so on.  I customized one of my previous iLogic rules to be similar to yours, because I do not put exported files into sub folders, and do not launch the folder after export.  My code process also usually creates a sketch on the face, with all the faces edges projected into it, then export that sketch using the PlanarSketch.DataIO.WriteDataToFile route, then aborts the transaction to eliminate the sketch, leaving the DXF file alone.  Below is that edited code example, which has several checks and feedbacks included.  Perhaps the most informative one is the 'Logger' entry, which writes the proposed FullFileName of the new DXF to the iLogic Log window (only effective if that tab is visible before running the rule).  The other small detail to notice is the 'DoEvents' calls between the two commands near the end, to allow Inventor to finish processing stuff before the next command is processed.

Dim oInvApp As Inventor.Application = ThisApplication
Dim oDoc As Inventor.Document = oInvApp.ActiveDocument
If oDoc Is Nothing Then Return
If (Not TypeOf oDoc Is PartDocument) AndAlso (Not TypeOf oDoc Is AssemblyDocument) Then
	MsgBox("Active Document Is Not An Assembly or Part!", vbCritical, "")
	Return
End If
'<<< verify pre-selection >>>
Dim oSS As Inventor.SelectSet = oDoc.SelectSet
If oSS.Count = 0 Then
	MsgBox("Nothing Pre-Selected!", vbCritical, "")
	Return
ElseIf oSS.Count > 1 Then
	MsgBox("Multiple Objects Pre-Selected!", vbCritical, "")
	Return
End If
'try to Cast the first generic Object to a Face type (no error on failure)
Dim oFace As Inventor.Face = TryCast(oSS.Item(1), Inventor.Face)
'make sure the variable got assigned a value
If oFace Is Nothing Then
	MsgBox("No Face Pre-Selected!", vbCritical, "")
	Return
End If
'<<< now formulate the full path and file name of the new DXF file >>>
If oDoc.FileSaveCounter = 0 Then
	MsgBox("This Document has not been saved yet, so it has no 'Path' or 'FileName'!", vbCritical, "")
	Return
End If
Dim sFFN As String = oDoc.FullFileName
'gets the full path, without file name
Dim sPath As String = System.IO.Path.GetDirectoryName(sFFN)
'combine that path with sub folder name
sPath = System.IO.Path.Combine(sPath, "DXF")
'if that folder does not exist, create it
If Not System.IO.Directory.Exists(sPath) Then
	System.IO.Directory.CreateDirectory(sPath)
End If
'get just the file name, without path, and without file extension
Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(sFFN)
'combine path and new file name for full file name of new DXF
sFFN = System.IO.Path.Combine(sPath, sFileName & ".dxf")
Logger.Info("Proposed FullFileName of new DXF:" & vbCrLf & sFFN)

Dim oCmdMgr As Inventor.CommandManager = oInvApp.CommandManager
'send data to Inventor's memory
oCmdMgr.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, sFFN)
'give Inventor time to process that task
oInvApp.UserInterfaceManager.DoEvents()
'capture Face Edges, and create DXF from them (normally shows SaveAs dialog)
oCmdMgr.ControlDefinitions.Item("GeomToDXFCommand").Execute()
'give Inventor time to process that task
oInvApp.UserInterfaceManager.DoEvents()
'open folder where file was created
Shell("explorer.exe " & sPath, vbNormalFocus)

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes