Export Face As DXF

Export Face As DXF

mitchELAHB
Advocate Advocate
3,257 Views
9 Replies
Message 1 of 10

Export Face As DXF

mitchELAHB
Advocate
Advocate

Hi,

 

I'm trying to export a face from a solid using iLogic. I have made the face a named entity and have been trying to export the face using the GeomToDXFCommand API, but have had no luck in being able to pass the face to the API. I can't export the flat pattern as I only want the face in its folded form. Is there a way to get the functionality of 'Right Click' > 'Export Face As' using iLogic? 

 

Thanks

0 Likes
Accepted solutions (2)
3,258 Views
9 Replies
Replies (9)
Message 2 of 10

gerrardhickson
Collaborator
Collaborator
Accepted solution

Try this:

It requires you to have a face selected before running. You could select this face programmatically or manually.

 

 

Sub Main()

Dim oCtrlDef As ButtonDefinition
oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")

Dim oDXFFullFileName As String
oDXFFullFileName = "C:\Temp\Face.dxf"

ThisApplication.CommandManager.PostPrivateEvent( PrivateEventTypeEnum.kFileNameEvent, oDXFFullFileName)

oCtrlDef.Execute

End Sub

 

 

Borrowed from here:

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/api-quot-export-face-as-dxf-quot-in-...

Message 3 of 10

mitchELAHB
Advocate
Advocate
Accepted solution

Thanks, I've implemented something similar with a try/catch statement when setting the face name.

 

 

Function SaveFace(FaceName As String, SavePath As String)	

	'finding entity called 'FaceName', providing error if can't find
	Dim oNamedEntities = iLogicVb.Automation.GetNamedEntities(ThisDoc.Document)
	Try
		oFace = oNamedEntities.FindEntity(FaceName)
	Catch
		MessageBox.Show("Cannot find face called '" & FaceName & "'! DXF not saved.", "Error saving file")
		Exit Function
	End Try
	
	'selecting entity called 'FaceName'
	ThisDoc.Document.SelectSet.Select(oFace)
	
	'Export selected face to 'SavePath'	
	Dim oCmdMgr As CommandManager = ThisApplication.CommandManager
	Dim oCtrlDef As ButtonDefinition = oCmdMgr.ControlDefinitions.Item("GeomToDXFCommand")
	oCmdMgr.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, SavePath)
	oCtrlDef.Execute

End Function

 

 

This works well for my needs, but I did wonder if there was a more elegant solution than selecting the face programmatically. 

 

Cheers

 

 

 

Message 4 of 10

gerrardhickson
Collaborator
Collaborator

Do you mean you would like to export the face directly? E.g. oFace.exporttodxf? 

If that's what you were hoping for, then it seems that you're out of luck. I think the exit face as DXF was introduced to inventor relatively recently, so it hasn't been included in the API yet.

0 Likes
Message 5 of 10

mitchELAHB
Advocate
Advocate

Thanks for that. Yeah, I was hoping to be able to do some a bit more eloquently, but what I have works.

Cheers

Message 6 of 10

MSt-Louis
Contributor
Contributor
Thanks for sharing this code.
Very helpful for me.
🙂
Message 7 of 10

MSt-Louis
Contributor
Contributor

@mitchELAHB , it works great.

Instead of exporting a face from "ThisDoc", I would like to export a face from a known file, because I want to run this rule in a DWG file to export faces from multiple .ipt files that are there.

Eg. 

'selecting entity called 'FaceName'
ThisDoc.Document.SelectSet.Select(oFace)

How can I change this line to select oFace from a specific known file?

Thank you.

0 Likes
Message 8 of 10

mitchELAHB
Advocate
Advocate

Hi,

Would something like this do? 

 

 

 

Sub main()
	
	Dim sFilePath As String = "C:\TEST\Test.ipt"
	Dim oDoc As Document = ThisApplication.Documents.Open(sFilePath, True) 	'False = open invisible, True = open visible. invisible is much faster.
	Dim oFaceName As String = "Test" 
	Dim oSavePath As String = "C:\TEST\Test.dxf"
	SaveFace(oDoc, oFaceName, oSavePath)
	oDoc.Close
	
End Sub

Function SaveFace(oDoc As Document, FaceName As String, SavePath As String)	

	'finding entity called 'FaceName', providing error if can't find
	Dim oNamedEntities = iLogicVb.Automation.GetNamedEntities(oDoc)
	Try
		oFace = oNamedEntities.FindEntity(FaceName)
	Catch
		MessageBox.Show("Cannot find face called '" & FaceName & "'! DXF not saved.", "Error saving file")
		Exit Function
	End Try
	
	Dim oFolder As String = System.IO.Path.GetDirectoryName(SavePath)
	
	'creating folder if not exist
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If
	
	'selecting entity called 'FaceName'
	oDoc.SelectSet.Select(oFace)
	
	'Export selected face to 'SavePath'	
	Dim oCmdMgr As CommandManager = ThisApplication.CommandManager
	Dim oCtrlDef As ButtonDefinition = oCmdMgr.ControlDefinitions.Item("GeomToDXFCommand")
	oCmdMgr.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, SavePath)
	oCtrlDef.Execute

	'deselecting entity called 'FaceName'
	oDoc.SelectSet.Clear

End Function

 

 

 

 

Unfourtnately I think you'll need to open the files visibly to get the GeomToDXFCommand to work, which slows things down a little. I haven't tested this code much so you might need to add a few tweeks. 

Message 9 of 10

MSt-Louis
Contributor
Contributor

Hi,

Thank you for the code. Yes it works !

The only issue is here:

Dim oDoc As Document = ThisApplication.Documents.Open(sFilePath, True) 	'False = open invisible, True = open visible. invisible is much faster.

If I set the boolean to False (wich I think is a good idea to open invisible), the export does not take place. But I think you already knew this.

If I have no choice, I'll add oDoc.Close at the end of your code.

0 Likes
Message 10 of 10

mitchELAHB
Advocate
Advocate

Yes, as far as I know the document will need to be opened. Good point on closing the file, that slipped my mind, I'll update my code for any future searches that find this topic.