Export to dxf with custom iProperties in new file name

Export to dxf with custom iProperties in new file name

kresh.bell
Collaborator Collaborator
321 Views
3 Replies
Message 1 of 4

Export to dxf with custom iProperties in new file name

kresh.bell
Collaborator
Collaborator

Hi,

is it possible for someone to help me? I need an iLogic that would:
- works in an assembly environment
- all selected parts, export face to dxf (v 2010), if sheet metal then flat pattern, without bending line
- name as follows (file name)_(custom iPropertie MATERIAL_SIFRA)_(custom iPropertie DEB)mm_(number of pieces in assembly)x

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

JelteDeJong
Mentor
Mentor

This is a difficult question.

First of all, there is no way to select multiple documents that you want to export. Here I solved this by opening each file. if you don't select a face. (press escape) then it will skip the part.

Maybe a bigger problem there is no API for exporting faces. There is a workaround. You can execute commands as you clicked on them. this is unstable but might work for you. This is what the rule below does. (I also left the other 2 ways of exporting documents in the code for educational purposes)

Sub Main()
	Dim doc As AssemblyDocument = ThisDoc.Document

	Dim bom As BOM = doc.ComponentDefinition.BOM
	bom.PartsOnlyViewEnabled = True
	Dim bomView As BOMView = bom.BOMViews.Item("Parts Only")


	For Each row As BOMRow In bomView.BOMRows
		Try
			Dim refDoc As Document = row.ComponentOccurrences.Item(1).ReferencedDocumentDescriptor.ReferencedDocument
			If (refDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then
				Continue For
			End If

			Dim openDoc = ThisApplication.Documents.Open(refDoc.FullFileName)
			Dim newFileName As String = createFileName(openDoc, row.TotalQuantity)

			If (openDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then
				Dim defSheetMetal As SheetMetalComponentDefinition = openDoc.ComponentDefinition
				If defSheetMetal.HasFlatPattern = False Then
					defSheetMetal.Unfold()
				Else
					defSheetMetal.FlatPattern.Edit()
				End If

				exportFace(openDoc, newFileName)

				defSheetMetal.FlatPattern.ExitEdit()
			Else
				exportFace(openDoc, newFileName)
			End If
			openDoc.Close()



		Catch ex As Exception
			Try
				MsgBox("Problem with file: " &
							row.ComponentOccurrences.Item(1)._DisplayName &
						"Message: " & ex.Message)
			Catch
			End Try
		End Try
	Next

End Sub

Function createFileName(doc As PartDocument, quantity As String) As String
	Dim dirName As String = IO.Path.GetDirectoryName(doc.FullFileName)
	Dim fileNameWithoutExtension As String = IO.Path.GetFileNameWithoutExtension(doc.FullFileName)

	Dim propSet As PropertySet = doc.PropertySets.Item("Inventor User Defined Properties")
	Dim MATERIAL_SIFRA = GetPropValue(propSet, "MATERIAL_SIFRA")
	Dim DEB = GetPropValue(propSet, "DEB")

	Return String.Format("{0}\{1}_{2}_{3}mm_{4}x.dxf",
	dirName, fileNameWithoutExtension, MATERIAL_SIFRA, DEB, quantity)
End Function
Function GetPropValue(propSet As PropertySet, name As String) As String
	Try
		Return propSet.Item(name).Value
	Catch ex As Exception
		Return "~Unknown~"
	End Try
End Function
Sub exportFace(doc As PartDocument, newFileName As String)
	Dim face As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select a face to export. Esc to skip")
	If face Is Nothing Then Return

	' Many options but does not work for faces
	'Dim def As PartComponentDefinition = doc.ComponentDefinition

	'Dim sketch = def.Sketches.Add(face, True)

	'Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2010&InvisibleLayers=BendUpLayer;BendDownLayer"

	'sOut = ""

	'sketch.DataIO.WriteDataToFile(sOut, newFileName)

	'sketch.Delete()
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	' Many options but in a seperate file. Does not work with faces
	'Dim DXFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")

	'Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
	'oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

	'Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

	'Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium

	'If DXFAddIn.HasSaveCopyAsOptions(doc, oContext, oOptions) Then

	'	Dim strIniFile = "C:\temp\DxfExport.ini" 'this is a pre-configed ini file
	'	oOptions.Value("Export_Acad_IniFile") = strIniFile

	'	oDataMedium.FileName = newFileName
	'	DXFAddIn.SaveCopyAs(face, oContext, oOptions, oDataMedium)
	'End If
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

	' no options posible but works for faces.
	Dim oCtrlDef As ButtonDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")
	doc.SelectSet.Select(Face)
	ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, newFileName)
	oCtrlDef.Execute()
End Sub

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 4

kresh.bell
Collaborator
Collaborator

Hi,

thank you very much, sorry to deelay, I was sick.

It works great, face selection is not a problem. Is it possible to somehow limit the participating parts? For example by selection.

I often have a case when I have an assembly with many parts and I only need a few dxf faces from them

0 Likes
Message 4 of 4

kresh.bell
Collaborator
Collaborator

Hi,

does anyone have an idea how to limit this excellent iLogic to selected parts?

0 Likes