Help: Code For "Export face as". Have tried everything i know

Help: Code For "Export face as". Have tried everything i know

xenocatalyst
Advocate Advocate
5,281 Views
27 Replies
Message 1 of 28

Help: Code For "Export face as". Have tried everything i know

xenocatalyst
Advocate
Advocate

Hello,

 

I have been trying to write an iLogic rule to automate the process of exporting a selected face to a dxf file.

The file name and location is set using information from iproperties and project values.

I have searched this forum and the web in general but cant seem to find an answer to my problem.

My background in coding is from autocad lisp (enough to get me by), maybe this is confusing my understanding of iLogic.

Maybe the code i have found is for VB and not iLogic.

With all the examples i have referenced I have been unable to see any link between the select face function and the export function.

How does "GeomToDXFCommand" know what face you have selected?

 

With the code I have everything seems to work except the actual export function.

 

 

When i run the rule from the iLogic Browser (right click, run), i get prompted to select a face.

I select the face i want to export and then nothing.

 

If I then right click and select "Export Face As" a dxf file with the correct filename is created in the project folder in a folder called "DXF".

This happens without and dialog box or prompts, as I would expect if the ilogic rule ran correctly.

 

Any help with coding or my understanding of the code would be greatly appreciated.

 

Here is my code, it is a work in progress so it may be a bit untidy.

I have also attached the ilogic file.

 

 

'check that this active document is a part file    

'[ ;Part Check
Imports System.Windows.Forms
If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
MessageBox.Show ("Please open a part document", "iLogic")
End If
']

'[ ;Set Variables
oPath = ThisDoc.Path

''Set a reference to the active document (the document to be published).
Dim oDocument As Document = ThisApplication.ActiveDocument
	
'' Create a DataMedium object
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
	oQty = iProperties.Value(docFName, "Project", "Stock Number")
	oMat = iProperties.Value(docFName, "Summary", "Comments")
	oFolder = oPath & "\DXF\"
	oFileName = ThisDoc.FileName(False) & " - "& oMat & " - " & oQty & " Required.dxf" 'without extension

''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

''Set the dxf target file name
oDataMedium.FileName = oFolder & oFilename 
DefaultChoice = True
'MessageBox.Show(oDataMedium.FileName)
''copies filename to clipboard for paste (ctrl + v)
'Clipboard.SetText(oFileName)
']


'[;Selection Code
Dim testFace As Face 
                
                 Try
                 testFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face") 
                 Catch  ex As Exception
                 End Try
            
                    If (testFace Is Nothing) Then
                        Return
                    End If
']

'[;Export code 1

'Get CommandManager object
'Dim Cm As CommandManager
'Cm = ThisApplication.CommandManager
Dim Cm As CommandManager = ThisApplication.CommandManager
Cm.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oDataMedium.FileName)


'Get control definition for the line command. 
'Dim oCDef As ControlDefinition = Cm.ControlDefinitions.Item( "GeomToDXFCommand")
'oCDef.Execute()

'''at this point i can right click on face and select 'Export as face' and it will be saved properly
'''without any further input.
'''Its Almost as if the selected face isn't being parsed to the GeomToDXFCommand.
']

'[;Clear Variables
oPath = Nothing
oDocument = Nothing
oDataMedium = Nothing
oQty = Nothing
oMat = Nothing
oFolder = Nothing
oFileName = Nothing
testFace = Nothing

']

 

 

Accepted solutions (2)
5,282 Views
27 Replies
Replies (27)
Message 2 of 28

MechMachineMan
Advisor
Advisor

Why are these 2 lines commented out in your code?

 

'Dim oCDef As ControlDefinition = Cm.ControlDefinitions.Item( "GeomToDXFCommand")
'oCDef.Execute()

 They are the 2 that actually perform the control definition function.


--------------------------------------
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 3 of 28

xenocatalyst
Advocate
Advocate

These were commented out while i was testing, they don't seem to make a difference to the outcome however.


@MechMachineMan wrote:

Why are these 2 lines commented out in your code?

 

'Dim oCDef As ControlDefinition = Cm.ControlDefinitions.Item( "GeomToDXFCommand")
'oCDef.Execute()

 They are the 2 that actually perform the control definition function.


 

0 Likes
Message 4 of 28

xenocatalyst
Advocate
Advocate

Through testing I have discovered that the problem does not lie in the export code but in the selection code.

 

 

'Selection Code
Dim testFace As Face 
                
      Try
      testFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face") 
      Catch  ex As Exception
      End Try
            
      If (testFace Is Nothing) Then
      Return
      End If

 

 

While this code indeed allows me to select the appropriate face it doesn't hold onto the the face as a pre-selection.

 

If i select the face prior to running the ilogic rule it all works fine but i would prefer the selection to be handled within the rule.

 

Can anyone help me remedy this?

 

0 Likes
Message 5 of 28

frederic.vandenplas
Collaborator
Collaborator
Accepted solution

I guess (without testing) that you need to add the face to a selectset before exporting

This is normal behaviour just as in this post that i solved Face Parallel to display

Without adding it to a selectset, the command "Look at" isn't excuted as expected.

 

Dim oSelSet As SelectSet
Set oSelSet = oDoc.SelectSet
Dim oFace As Face
Set oFace = ThisApplication.CommandManager.Pick(kPartFacePlanarFilter, "Select a face.")
If oFace Is Nothing Then
Exit Sub
Else
Call oSelSet.Select(oFace)
End If

 

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
Message 6 of 28

xenocatalyst
Advocate
Advocate

Hello Frederic,

 

Thank you for the answer.

 

I have included the the code as per your example and it is now working fantastically.

 

 

0 Likes
Message 7 of 28

frederic.vandenplas
Collaborator
Collaborator

Glad to here that!

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Message 8 of 28

Anonymous
Not applicable

can I have the actually code..i tried to copy and paste and I get error saying 'set' and 'lets' assignment statements are no longer supported..i am no means a programmer.. 🙂

0 Likes
Message 9 of 28

xenocatalyst
Advocate
Advocate

Hi jsmanrique 

 

I'm not really a programmer either. I have done qbasic and website production but those skills are 20 year out of date now.

 

I have had problems with trying bits of code I have found throughout the internet and have found that people don't know or don't tell you if the code is ilogicvb, vba, or vb.net.

 

The code I'm working in is iLogicvb.

 

The code frederic.vandenplas posted was vba. (as in macros)

While Frederic's code wasn't exactly correct for ilogic, it gave me the method that I was missing.

Which was adding the face to the selection set. ie: oSelSet.Select(oFace)

 

I now have my code working in iLogicVB and as a Macro(VBA).

I had to add in the "Let" and "Set" statements for it to work in VBA.

 

My next step is to create an Inventor Addin. I though If I could get the code to work in VBA that it would be easy to transfer to VB.net.

However I have learnt that VBA is an old revision of visual basic and VB.NET is the current version, and that iLogic is actually closer to VB.net than VBA.

 

Sorry for the long winded explaination but if I say it here it may help many others.

 

Here is a working version of my code. I hope it helps.

 

 

'check that this active document is a part file 
Imports System.Windows.Forms
If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
MessageBox.Show ("Please open a part document", "iLogic")
End If
''Set Variables
'Set a reference to the active document
Dim oDoc As PartDocument
	oDoc = ThisApplication.ActiveDocument
	oQty = iProperties.Value(docFName, "Project", "Stock Number")
	oMat = iProperties.Value(docFName, "Summary", "Comments")
	oFolder = ThisDoc.Path & "\DXF\"
	oFileName = ThisDoc.FileName(False) & " - "& oMat & " - " & oQty & " Required.dxf" 'without extension
'add filename to memory for file save dialog
Dim Cm As CommandManager
	Cm = ThisApplication.CommandManager
	Cm.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oFolder & oFilename )
Dim oSelectSet As Selectset
	oSelectSet = oDoc.SelectSet
oDoc.SelectSet.Clear()


'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

'SELECTION CODE
Dim oFace As Face 
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face") 
If (oFace Is Nothing) Then
Return
Else
Call oDoc.SelectSet.Select(oFace)
End If

' EXPORT CODE 1
Dim oCtrlDef As ButtonDefinition
oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")
Call oCtrlDef.Execute


'CLEAR selectset
oDoc.SelectSet.Clear()

 

Message 10 of 28

Anonymous
Not applicable
Thanks so much for the quick reply!! works great, just need to fool around with dxf file name and locations..great job
0 Likes
Message 11 of 28

fabiorcferreira
Contributor
Contributor

Can you make it so the file saves as dwg instead of dxw?

Message 12 of 28

ithelpdesk
Explorer
Explorer

Hi,     i am trying to automate the process.can any one help me.

1)I have an Assembly file with all parts.

2) Select the part which as description starting "PL". .

3) open the part and select the face export to ".dxf and close

4) if the part is sheet metal then unfold and export face as ".dxf Note: the face which is to be exported may be part file or sheet metal file. kindly help me to solve this.  

thanks

Sri    

0 Likes
Message 13 of 28

3DAli
Collaborator
Collaborator

Hi ,

 

this post helped me a lot and I wanted to thank you for that, 

I am facing a similar issue when I add a face to my select set not using pick method. I tested my code, my select set is not empty and the face is added , but the export face command does not produce any result. I have a piece of code that automatically identifies the biggest planar face in a part, then I want to export that face and it fails.

when I run it, it runs without any error, and it gives me the confirmation message , but the DXF folder is empty.

 

any idea as to what could be a work around solution to select a face in any other ways than Pick method?

 

Regards

Ali

 

Sub Face_to_dxf(BFace As Face)
        
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument

Dim oDef As PartComponentDefinition
Set oDef = oDoc.ComponentDefinition


Dim oSelect As SelectSet
Set oSelect = oDoc.SelectSet


Call oSelect.Select(BFace)



oFolder = "C:\DXF\"
oFilename = "Test.dxf" 'without extension
'add filename to memory for file save dialog
Dim Cm As CommandManager
Set Cm = ThisApplication.CommandManager
Call Cm.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oFolder & oFilename)
 

 
 
 
 If oSelect.Count > 0 Then
  
  If oSelect.Item(1).Type = kFaceObject Then
   
   'Call Save as dxf

   Dim oCtrlDef As ButtonDefinition
   Set oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")
   Call oCtrlDef.Execute
   
   MsgBox "Biggest Face Exported!", vbInformation, "Export to dxf"
   Call oDoc.SelectSet.Clear
   
   Else
    
    MsgBox "No area was choosen!", vbCritical, "Export to dxf"
    Call oDoc.SelectSet.Clear
    Exit Sub
  
  End If
 
 End If
End Sub

  

0 Likes
Message 14 of 28

MechMachineMan
Advisor
Advisor
Try reselecting the face (using the variable and oDoc.Select) immediately
before running the command.

--------------------------------------
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 15 of 28

3DAli
Collaborator
Collaborator

@MechMachineMan 

 

I tried it , unfortunately didn't resolve the issue. I also found this in another post and I don't know how to use it, I am playing around with it as we speak.

 

"A SelectSet deals with objects selected in the graphics window through user
interaction. If you know the objects you want programmatically, just add
them to a Collection or IV ObjectCollection
(IvApp.TransientObjects.CreateObjectCollection)"

 

if you know how to use object collection method , can you please post an example.

 

Cheers

Ali

0 Likes
Message 16 of 28

xenocatalyst
Advocate
Advocate
Hi MechMachineMan,
You need to define Bface somehow before you can add it to the selectset.

That is what I used the commandmanager.pick method to do.
0 Likes
Message 17 of 28

3DAli
Collaborator
Collaborator

BFace is defined using the following code, then I call the sub above 

 

Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
                            
Dim oDef As PartComponentDefinition
Set oDef = oPartDoc.ComponentDefinition 

Dim f As Face
  
Dim bf As Face

Dim area As Double

  For Each f In oDef.SurfaceBodies(1).Faces
    ' Only care about planar faces
    If TypeOf f.Geometry Is Plane And f.Evaluator.area > area Then
      Set bf = f
      area = f.Evaluator.area
    End If
  Next

 

 

0 Likes
Message 18 of 28

xenocatalyst
Advocate
Advocate

Shouldn't your variable "bf" be "BFace" to match the variable in your sub.

0 Likes
Message 19 of 28

3DAli
Collaborator
Collaborator

not necessarily, you can call the sub like this

Call Face_to_dxf(bf)

 

, anyways, this is not what causes the issue, the problem is with selection method

0 Likes
Message 20 of 28

xenocatalyst
Advocate
Advocate

I just ran this though iLogic and it works well.

I added some code to check if the directory exists and I believe that was the only issue I had.

 

Here is the code I used

Imports System.Windows.Forms
Sub Main()
Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument                     
Dim oDef As PartComponentDefinition = oPartDoc.ComponentDefinition
Dim f As Face
Dim bf As Face

Dim area As Double
  For Each f In oDef.SurfaceBodies(1).Faces
    ' Only care about planar faces
    If TypeOf f.Geometry Is Plane And f.Evaluator.Area > area Then
      bf = f
      area = f.Evaluator.Area
    End If
  Next
Call Face_to_dxf(bf)
End Sub

Sub Face_to_dxf(BFace As Face)
        
Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim oSelect As SelectSet = oDoc.SelectSet
Call oSelect.Select(BFace)

oFolder = "C:\DXF\"
oFilename = "Test.dxf" 'without extension
'add filename to memory for file save dialog

'[added this check
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If
']

Dim Cm As CommandManager = ThisApplication.CommandManager
Call Cm.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oFolder & oFilename)
  
 If oSelect.Count > 0 Then
   If oSelect.Item(1).Type = kFaceObject Then
	  
	   Dim oCtrlDef As ButtonDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")
	   Call oCtrlDef.Execute
	   
	   MessageBox.Show("Biggest Face Exported!", "Export to dxf")
	   Call oDoc.SelectSet.Clear
	   
   Else
    MessageBox.Show("No area was choosen!", "Export to dxf")
    Call oDoc.SelectSet.Clear
    Exit Sub
  
  End If
 
 End If
End Sub

 

0 Likes