Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results forย 
Showย ย onlyย  | Search instead forย 
Did you mean:ย 

DXF name generate by I properties

22 REPLIES 22
SOLVED
Reply
Message 1 of 23
jwoutersJC4LN
944 Views, 22 Replies

DXF name generate by I properties

jwoutersJC4LN
Contributor
Contributor

Hi,

 

I'm looking for a code what can generate an Flatpattern DXF automatically by saving the Sheet-Metal file.

the name of the DXF must contain the following Iproperties

 

- Partnumber (Iproperties, Project, Partnumber)

-SheetmetalDefaults Thickness

-SheetmetalDefaults Material

-Comments (Iproperties, Summary, Comments)

-Project (Iproperties, Project, Project)

-Vendor (Iproperties, Project, Vendor)

 

Hope something like this is possible. I'm not a programmer but hope someone here can help us ๐Ÿ™‚

 

Regards,

 

Justin 

0 Likes

DXF name generate by I properties

Hi,

 

I'm looking for a code what can generate an Flatpattern DXF automatically by saving the Sheet-Metal file.

the name of the DXF must contain the following Iproperties

 

- Partnumber (Iproperties, Project, Partnumber)

-SheetmetalDefaults Thickness

-SheetmetalDefaults Material

-Comments (Iproperties, Summary, Comments)

-Project (Iproperties, Project, Project)

-Vendor (Iproperties, Project, Vendor)

 

Hope something like this is possible. I'm not a programmer but hope someone here can help us ๐Ÿ™‚

 

Regards,

 

Justin 

22 REPLIES 22
Message 2 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.

There a few things you may need to keep in mind about this request/project...

  • Is it possible that any sheet metal part may not have all of those iProperties filled in?
    • If so, how should that effect the file's name?
    • Should it not export the DXF and/or alert the user when that happens?
  • Is it possible that certain characters may be contained within all those values that either Inventor or your filing system may not like within a file's name?
    • For example:  dots\periods, commas, slashes(/ or \), or other special characters that may not be allowed.
  • Is it possible that the file's name may become too long?
    • I have encountered this issue at our facility.  This problem is more common when storing your files on a network drive, and the overall length of the combined path & name of the file, all the way back to the host computer's name can become too long.
  • Should the data be in the same order as you have listed, or in some other specific order?
  • Do you want a specific separator character and/or spaces left between the different pieces of data in the name?
    • If so, which characters, and between which pieces of data?  Maybe an example would be in order?

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @jwoutersJC4LN.

There a few things you may need to keep in mind about this request/project...

  • Is it possible that any sheet metal part may not have all of those iProperties filled in?
    • If so, how should that effect the file's name?
    • Should it not export the DXF and/or alert the user when that happens?
  • Is it possible that certain characters may be contained within all those values that either Inventor or your filing system may not like within a file's name?
    • For example:  dots\periods, commas, slashes(/ or \), or other special characters that may not be allowed.
  • Is it possible that the file's name may become too long?
    • I have encountered this issue at our facility.  This problem is more common when storing your files on a network drive, and the overall length of the combined path & name of the file, all the way back to the host computer's name can become too long.
  • Should the data be in the same order as you have listed, or in some other specific order?
  • Do you want a specific separator character and/or spaces left between the different pieces of data in the name?
    • If so, which characters, and between which pieces of data?  Maybe an example would be in order?

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 23

Michael.Navara
Advisor
Advisor

This sample builds dxf file name. It omits empty property values, thickness value (double) formatting and another possible issues mentioned by @WCrihfield .

 

'- Partnumber (iProperties, Project, Partnumber)
'-SheetmetalDefaults Thickness
'-SheetmetalDefaults Material
'-Comments (iProperties, Summary, Comments)
'-Project (iProperties, Project, Project)
'-Vendor (iProperties, Project, Vendor)

Dim part As PartDocument = ThisDoc.Document
Dim smCompDef As SheetMetalComponentDefinition = part.ComponentDefinition


'Get values for file name parts
Dim partNumber As String = part.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").Value.ToString()
Dim thickness As String = smCompDef.Thickness.ModelValue.ToString()
Dim material As String = smCompDef.ActiveSheetMetalStyle.Material.Name
Dim comments = part.PropertySets("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}")("Comments").Value.ToString()
Dim project As String = part.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Project").Value.ToString()
Dim vendor As String = part.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Vendor").Value.ToString()

'Create DXF file name with given placeholders
Dim dxfFileName = String.Format("{0}-{1}-{2}-{3}-{4}-{5}.dxf", _
	partNumber, _
	thickness, _
	material, _
	comments, _
	project, _
	vendor _
	)

Logger.Info(dxfFileName)

 

0 Likes

This sample builds dxf file name. It omits empty property values, thickness value (double) formatting and another possible issues mentioned by @WCrihfield .

 

'- Partnumber (iProperties, Project, Partnumber)
'-SheetmetalDefaults Thickness
'-SheetmetalDefaults Material
'-Comments (iProperties, Summary, Comments)
'-Project (iProperties, Project, Project)
'-Vendor (iProperties, Project, Vendor)

Dim part As PartDocument = ThisDoc.Document
Dim smCompDef As SheetMetalComponentDefinition = part.ComponentDefinition


'Get values for file name parts
Dim partNumber As String = part.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").Value.ToString()
Dim thickness As String = smCompDef.Thickness.ModelValue.ToString()
Dim material As String = smCompDef.ActiveSheetMetalStyle.Material.Name
Dim comments = part.PropertySets("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}")("Comments").Value.ToString()
Dim project As String = part.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Project").Value.ToString()
Dim vendor As String = part.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Vendor").Value.ToString()

'Create DXF file name with given placeholders
Dim dxfFileName = String.Format("{0}-{1}-{2}-{3}-{4}-{5}.dxf", _
	partNumber, _
	thickness, _
	material, _
	comments, _
	project, _
	vendor _
	)

Logger.Info(dxfFileName)

 

Message 4 of 23
jwoutersJC4LN
in reply to: WCrihfield

jwoutersJC4LN
Contributor
Contributor

Hi 

  • Is it possible that any sheet metal part may not have all of those iProperties filled in?

yes this is possible.. it would be great if it generate a dxf anyway with all the properties wich are filled in.

 

  • Is it possible that certain characters may be contained within all those values that either Inventor or your filing system may not like within a file's name?

If it's possible, would be nice to have a - between the properties. Like Material-Thickness-etc..

 

  • Is it possible that the file's name may become too long?

Yes this is possible. we've had this a few times before. Is it possible to shorten the DXF name Partnumber to the last 6 digits?

 

  • Should the data be in the same order as you have listed, or in some other specific order?

I think in the same folder as the assembly is stored or something like that. Or maybe a reference to the project folder?

 

  • Do you want a specific separator character and/or spaces left between the different pieces of data in the name?If so, which characters, and between which pieces of data?  Maybe an example would be in order?

an example of what we do use know is this;  Material-thickness-Qt-Client-Ordernumber. This is all done by hand know but all the things already filled in the BOM structure in an assembly. 

this code can be read by our machine. 

 

Like to see your response!

 

Thanks so far and kind regards

Justin

 

 

0 Likes

Hi 

  • Is it possible that any sheet metal part may not have all of those iProperties filled in?

yes this is possible.. it would be great if it generate a dxf anyway with all the properties wich are filled in.

 

  • Is it possible that certain characters may be contained within all those values that either Inventor or your filing system may not like within a file's name?

If it's possible, would be nice to have a - between the properties. Like Material-Thickness-etc..

 

  • Is it possible that the file's name may become too long?

Yes this is possible. we've had this a few times before. Is it possible to shorten the DXF name Partnumber to the last 6 digits?

 

  • Should the data be in the same order as you have listed, or in some other specific order?

I think in the same folder as the assembly is stored or something like that. Or maybe a reference to the project folder?

 

  • Do you want a specific separator character and/or spaces left between the different pieces of data in the name?If so, which characters, and between which pieces of data?  Maybe an example would be in order?

an example of what we do use know is this;  Material-thickness-Qt-Client-Ordernumber. This is all done by hand know but all the things already filled in the BOM structure in an assembly. 

this code can be read by our machine. 

 

Like to see your response!

 

Thanks so far and kind regards

Justin

 

 

Message 5 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.  Although the code that @Michael.Navara provided above is likely just fine for generating the complicated part of the DXF's name, since you asked to see my response, here it is.  I assume you are going to use this code as an external ilogic rule, instead of a local/internal iLogic rule.  However, if you want to use it as a local/internal rule, we might be able to shorten & simplify the code a bit in a few places.  I also assume your Inventor version is within the last few years, because I am using the iLogic Logger in a couple places.  If you are using an older version, you may have to replace those with something like a MsgBox or comment them out.  In my code below am using the file path of the sheet metal part itself as the path of the DXF file that is to be created, because I did not know what assembly you were talking about in your response.  Also, I am taking the opportunity to make sure that the Thickness is in the parameter's own units, not in 'database' units, just in case they are not the same units.  When retrieving the Value or ModelValue from an API Parameter object, the value is always in 'database' units, which is centimeters for lengths.  I am also taking care with some extra code to not have "-" marks in the file's name, where there might not be anything to put between them.  If this is not necessary, or if you need those marks in there even if no values are entered between them, that can easily be fixed and will reduce the code's size a bit.  I put the actual export step down into its own separate Sub routine, so that it is more modular, and can be used in other places.  Within that 'ExportFlatPatternToDXF' Sub, you will want to edit the input options in the oFormat variable.  The available options are specified on the online help page for the DataIO object (Link1, Link2).  Right now, I am just specifying that it should be an AutoCAD 2000 style DXF.

Here is what I have for you right now:

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisDoc.Document
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	Dim oPath As String = System.IO.Path.GetDirectoryName(oPDoc.FullFileName)
	'gather values for use in DXF name
	Dim oProjSet As PropertySet = oPDoc.PropertySets.Item("Design Tracking Properties")
	Dim oPN As String = oProjSet.Item("Part Number").Value
	Dim oProject As String = oProjSet.Item("Project").Value
	Dim oVendor As String = oProjSet.Item("Vendor").Value
	Dim oComments As String = oPDoc.PropertySets.Item("Inventor Summary Information").Item("Comments").Value
	Dim oMaterial As String = oSMDef.ActiveSheetMetalStyle.Material.Name
	'get Thickness in 'document' units, instead of 'database' units, if they are different units
	Dim oThkParam As Inventor.Parameter = oSMDef.Thickness
	Dim oUOM As UnitsOfMeasure = oPDoc.UnitsOfMeasure
	Dim oThicknessVal As Double
	If oThkParam.Units <> "cm" Then
		oThicknessVal = oUOM.ConvertUnits(oThkParam.Value, "cm", oThkParam.Units)
	Else
		oThicknessVal = oThkParam.Value
	End If
	Dim oThickness As String = CStr(oThicknessVal)
	'start assembling the file name
	Dim oDXFFileName As String = oPN
	If oDXFFileName = "" Then oDXFFileName = oThickness Else oDXFFileName = oDXFFileName & "-" & oThickness
	oDXFFileName = oDXFFileName & "-" & oMaterial
	If oComments <> "" Then oDXFFileName = oDXFFileName & "-" & oComments
	If oProject <> "" Then oDXFFileName = oDXFFileName & "-" & oProject
	If oVendor <> "" Then oDXFFileName = oDXFFileName & "-" & oVendor
	oDXFFileName = oPath & "\" & oDXFFileName & ".dxf"
	MsgBox("oDXFFileName = " & oDXFFileName,,"")
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try
			oSMDef.Unfold
			oSMDef.FlatPattern.ExitEdit
			oFP = oSMDef.FlatPattern
		Catch oEx As Exception
			Logger.Error("Unfold Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
			Exit Sub
		End Try
	End If
	ExportFlatPatternToDXF(oFP, oDXFFileName)
End Sub

Sub ExportFlatPatternToDXF(oFlatPattern As FlatPattern, oNewFullFileName As String)
	If IsNothing(oFlatPattern) Or oNewFullFileName = "" Then Exit Sub
	Dim oDataIO As DataIO = oFlatPattern.DataIO
	'<<<< FILL IN FORMAT SETTINGS THE WAY YOU WANT THEM >>>>
	Dim oFormat As String = "AcadVersion=2000"
	Try
		oDataIO.WriteDataToFile(oFormat, oNewFullFileName)
		Logger.Info("New DXF created at:" & vbCrLf & oNewFullFileName)
	Catch oEx As Exception
		Logger.Error("Export Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

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

Hi @jwoutersJC4LN.  Although the code that @Michael.Navara provided above is likely just fine for generating the complicated part of the DXF's name, since you asked to see my response, here it is.  I assume you are going to use this code as an external ilogic rule, instead of a local/internal iLogic rule.  However, if you want to use it as a local/internal rule, we might be able to shorten & simplify the code a bit in a few places.  I also assume your Inventor version is within the last few years, because I am using the iLogic Logger in a couple places.  If you are using an older version, you may have to replace those with something like a MsgBox or comment them out.  In my code below am using the file path of the sheet metal part itself as the path of the DXF file that is to be created, because I did not know what assembly you were talking about in your response.  Also, I am taking the opportunity to make sure that the Thickness is in the parameter's own units, not in 'database' units, just in case they are not the same units.  When retrieving the Value or ModelValue from an API Parameter object, the value is always in 'database' units, which is centimeters for lengths.  I am also taking care with some extra code to not have "-" marks in the file's name, where there might not be anything to put between them.  If this is not necessary, or if you need those marks in there even if no values are entered between them, that can easily be fixed and will reduce the code's size a bit.  I put the actual export step down into its own separate Sub routine, so that it is more modular, and can be used in other places.  Within that 'ExportFlatPatternToDXF' Sub, you will want to edit the input options in the oFormat variable.  The available options are specified on the online help page for the DataIO object (Link1, Link2).  Right now, I am just specifying that it should be an AutoCAD 2000 style DXF.

Here is what I have for you right now:

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisDoc.Document
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	Dim oPath As String = System.IO.Path.GetDirectoryName(oPDoc.FullFileName)
	'gather values for use in DXF name
	Dim oProjSet As PropertySet = oPDoc.PropertySets.Item("Design Tracking Properties")
	Dim oPN As String = oProjSet.Item("Part Number").Value
	Dim oProject As String = oProjSet.Item("Project").Value
	Dim oVendor As String = oProjSet.Item("Vendor").Value
	Dim oComments As String = oPDoc.PropertySets.Item("Inventor Summary Information").Item("Comments").Value
	Dim oMaterial As String = oSMDef.ActiveSheetMetalStyle.Material.Name
	'get Thickness in 'document' units, instead of 'database' units, if they are different units
	Dim oThkParam As Inventor.Parameter = oSMDef.Thickness
	Dim oUOM As UnitsOfMeasure = oPDoc.UnitsOfMeasure
	Dim oThicknessVal As Double
	If oThkParam.Units <> "cm" Then
		oThicknessVal = oUOM.ConvertUnits(oThkParam.Value, "cm", oThkParam.Units)
	Else
		oThicknessVal = oThkParam.Value
	End If
	Dim oThickness As String = CStr(oThicknessVal)
	'start assembling the file name
	Dim oDXFFileName As String = oPN
	If oDXFFileName = "" Then oDXFFileName = oThickness Else oDXFFileName = oDXFFileName & "-" & oThickness
	oDXFFileName = oDXFFileName & "-" & oMaterial
	If oComments <> "" Then oDXFFileName = oDXFFileName & "-" & oComments
	If oProject <> "" Then oDXFFileName = oDXFFileName & "-" & oProject
	If oVendor <> "" Then oDXFFileName = oDXFFileName & "-" & oVendor
	oDXFFileName = oPath & "\" & oDXFFileName & ".dxf"
	MsgBox("oDXFFileName = " & oDXFFileName,,"")
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try
			oSMDef.Unfold
			oSMDef.FlatPattern.ExitEdit
			oFP = oSMDef.FlatPattern
		Catch oEx As Exception
			Logger.Error("Unfold Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
			Exit Sub
		End Try
	End If
	ExportFlatPatternToDXF(oFP, oDXFFileName)
End Sub

Sub ExportFlatPatternToDXF(oFlatPattern As FlatPattern, oNewFullFileName As String)
	If IsNothing(oFlatPattern) Or oNewFullFileName = "" Then Exit Sub
	Dim oDataIO As DataIO = oFlatPattern.DataIO
	'<<<< FILL IN FORMAT SETTINGS THE WAY YOU WANT THEM >>>>
	Dim oFormat As String = "AcadVersion=2000"
	Try
		oDataIO.WriteDataToFile(oFormat, oNewFullFileName)
		Logger.Info("New DXF created at:" & vbCrLf & oNewFullFileName)
	Catch oEx As Exception
		Logger.Error("Export Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

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)

Message 6 of 23
jwoutersJC4LN
in reply to: WCrihfield

jwoutersJC4LN
Contributor
Contributor

Hi @WCrihfield ,

 

what a lot of information. Thanks so far. I'll test the code asap! what we prefer is an  "Customize User Command" in the SheetMetal template. By clicking on this command, the DXF should be generate. Don't know if this code alread does this? the "-" between the different properties is needed to separate the code for the cutting machine operator.. "_" is also an option for us. The Acad 2000 is fine for us! For inventor we've 2023 but that might not be a problem i think?

 

Thanks in advance!

 

Justin

0 Likes

Hi @WCrihfield ,

 

what a lot of information. Thanks so far. I'll test the code asap! what we prefer is an  "Customize User Command" in the SheetMetal template. By clicking on this command, the DXF should be generate. Don't know if this code alread does this? the "-" between the different properties is needed to separate the code for the cutting machine operator.. "_" is also an option for us. The Acad 2000 is fine for us! For inventor we've 2023 but that might not be a problem i think?

 

Thanks in advance!

 

Justin

Message 7 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.  After reading the comments in your last post, I changed the part of the code where it assembles the pieces of data into the file name from this:

Dim oDXFFileName As String = oPN
If oDXFFileName = "" Then oDXFFileName = oThickness Else oDXFFileName = oDXFFileName & "-" & oThickness
oDXFFileName = oDXFFileName & "-" & oMaterial
If oComments <> "" Then oDXFFileName = oDXFFileName & "-" & oComments
If oProject <> "" Then oDXFFileName = oDXFFileName & "-" & oProject
If oVendor <> "" Then oDXFFileName = oDXFFileName & "-" & oVendor

...to this:

Dim oDXFFileName As String = oPN & "-" & oThickness & "-" & oMaterial & "-" & _
oComments & "-" & oProject & "-" & oVendor

...that way it maintains the "-" marks in the name, even if one of the pieces of data were empty/blank.  I hope that helps some.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @jwoutersJC4LN.  After reading the comments in your last post, I changed the part of the code where it assembles the pieces of data into the file name from this:

Dim oDXFFileName As String = oPN
If oDXFFileName = "" Then oDXFFileName = oThickness Else oDXFFileName = oDXFFileName & "-" & oThickness
oDXFFileName = oDXFFileName & "-" & oMaterial
If oComments <> "" Then oDXFFileName = oDXFFileName & "-" & oComments
If oProject <> "" Then oDXFFileName = oDXFFileName & "-" & oProject
If oVendor <> "" Then oDXFFileName = oDXFFileName & "-" & oVendor

...to this:

Dim oDXFFileName As String = oPN & "-" & oThickness & "-" & oMaterial & "-" & _
oComments & "-" & oProject & "-" & oVendor

...that way it maintains the "-" marks in the name, even if one of the pieces of data were empty/blank.  I hope that helps some.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 23
jwoutersJC4LN
in reply to: WCrihfield

jwoutersJC4LN
Contributor
Contributor

I've put the code in the VBA Editor but it's not working..

 

This is what i have so far. Is it possible to make the code as a module so that i can import the module in once? 

Ther're also 2 Sub/Endsub rules. Is that possible?

 

Regards,

 

Justin

 

 

jwoutersJC4LN_0-1662009308223.png

 

0 Likes

I've put the code in the VBA Editor but it's not working..

 

This is what i have so far. Is it possible to make the code as a module so that i can import the module in once? 

Ther're also 2 Sub/Endsub rules. Is that possible?

 

Regards,

 

Justin

 

 

jwoutersJC4LN_0-1662009308223.png

 

Message 9 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.  I did not realize you would be trying to use this code within a VBA macro.  The code I posted is formatted for use in an iLogic rule, and is not directly compatible for use in a VBA macro.  I assume you want to put a button in your ribbon somewhere that will allow you to click it once to run the code.  If so, and you are using Inventor 2022 or earlier, you will need to use a VBA macro to accomplish that, however, there are two possible ways you could set that up.  You could just have some code in the macro that runs the iLogic rule.  Or we would have to translate all of the code from iLogic & VB.NET to VBA, so that it can be used within the VBA macro directly.  I personally choose to avoid any new developments which make use of VBA, because of the security risks, which is why Autodesk stopped including it in any new installations of Inventor for the past few years.  However, if you need help translating the code to VBA, I can help with that.  Also, yes, you can have multiple Sub routines in a VBA macro, but the top/main routine of the macro can not be a Function (only a Sub), and can not be set up to receive 'input variables'.  If it is a Function or is set-up to receive input variables, it will still do what it is designed to do, but it will not be recognized as a macro that can be represented by a macro button on your ribbon.

 

In VBA, you can not immediately set a value to a newly declared variable, on the same line, right after you create it.  You must set its value on another line.  Also you must use the keyword 'Set' at the start of a line of code that sets a variable = to a value.  This is true for most variables that represent objects, however Set is not required when setting a value to certain simple data type variables like String, Integer, Boolean, etc.  In VBA you also must use the keyword 'Call' in front of a line of code that is calling a Sub routine to run.  For example, when using a MsgBox to simply show a message to the user, and are not expecting a response (a question with yes/no is expecting a response), you must precede that line of code with 'Call', because in that case MsgBox resembles a Sub.  But when you are using MsgBox to ask a question, you are expecting a response, and therefore must use a variable to receive the returned value from the MsgBox, and do not need to precede that line with 'Call'.  Also, some codes that you can use in an iLogic rule can not be used within a VBA macro.  Not only due to the differences in coding language, but sometimes also because some common methods (Subs/Functions) & Objects/Types/Interfaces were likely defined within the ilogic add-in, so they are not immediately available within the VBA environment.  For instance, the Try...Catch block of code not recognized in VBA, so you have to use the older methods of 'handling' errors, like 'On Error Resume Next' & 'On Error GoTo 0'.  And the iLogic Logger is not directly recognized in the VBA environment, because it is defined within the iLogic add-in.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @jwoutersJC4LN.  I did not realize you would be trying to use this code within a VBA macro.  The code I posted is formatted for use in an iLogic rule, and is not directly compatible for use in a VBA macro.  I assume you want to put a button in your ribbon somewhere that will allow you to click it once to run the code.  If so, and you are using Inventor 2022 or earlier, you will need to use a VBA macro to accomplish that, however, there are two possible ways you could set that up.  You could just have some code in the macro that runs the iLogic rule.  Or we would have to translate all of the code from iLogic & VB.NET to VBA, so that it can be used within the VBA macro directly.  I personally choose to avoid any new developments which make use of VBA, because of the security risks, which is why Autodesk stopped including it in any new installations of Inventor for the past few years.  However, if you need help translating the code to VBA, I can help with that.  Also, yes, you can have multiple Sub routines in a VBA macro, but the top/main routine of the macro can not be a Function (only a Sub), and can not be set up to receive 'input variables'.  If it is a Function or is set-up to receive input variables, it will still do what it is designed to do, but it will not be recognized as a macro that can be represented by a macro button on your ribbon.

 

In VBA, you can not immediately set a value to a newly declared variable, on the same line, right after you create it.  You must set its value on another line.  Also you must use the keyword 'Set' at the start of a line of code that sets a variable = to a value.  This is true for most variables that represent objects, however Set is not required when setting a value to certain simple data type variables like String, Integer, Boolean, etc.  In VBA you also must use the keyword 'Call' in front of a line of code that is calling a Sub routine to run.  For example, when using a MsgBox to simply show a message to the user, and are not expecting a response (a question with yes/no is expecting a response), you must precede that line of code with 'Call', because in that case MsgBox resembles a Sub.  But when you are using MsgBox to ask a question, you are expecting a response, and therefore must use a variable to receive the returned value from the MsgBox, and do not need to precede that line with 'Call'.  Also, some codes that you can use in an iLogic rule can not be used within a VBA macro.  Not only due to the differences in coding language, but sometimes also because some common methods (Subs/Functions) & Objects/Types/Interfaces were likely defined within the ilogic add-in, so they are not immediately available within the VBA environment.  For instance, the Try...Catch block of code not recognized in VBA, so you have to use the older methods of 'handling' errors, like 'On Error Resume Next' & 'On Error GoTo 0'.  And the iLogic Logger is not directly recognized in the VBA environment, because it is defined within the iLogic add-in.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 23
jwoutersJC4LN
in reply to: WCrihfield

jwoutersJC4LN
Contributor
Contributor

Hi! 

 

I understand the miscommunication. We do use Inventor 23. I've never worked with ILogic but i think i found it. 

when i create a sheet with all the properties, it will make a flat pattern from it. But i can't find the DXF and i dont get the message "

New DXF created at; or Export Failed

 

as i can see in the messag box below, the material is written as generic, but i've choosen a material. This is a material from our own database. Does that affect the code maybe?

jwoutersJC4LN_0-1662098581901.png

 

This is the code so far; Is that correct?

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisDoc.Document
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	Dim oPath As String = System.IO.Path.GetDirectoryName(oPDoc.FullFileName)
	'gather values for use in DXF name
	Dim oProjSet As PropertySet = oPDoc.PropertySets.Item("Design Tracking Properties")
	Dim oPN As String = oProjSet.Item("Part Number").Value
	Dim oProject As String = oProjSet.Item("Project").Value
	Dim oVendor As String = oProjSet.Item("Vendor").Value
	Dim oComments As String = oPDoc.PropertySets.Item("Inventor Summary Information").Item("Comments").Value
	Dim oMaterial As String = oSMDef.ActiveSheetMetalStyle.Material.Name
	'get Thickness in 'document' units, instead of 'database' units, if they are different units
	Dim oThkParam As Inventor.Parameter = oSMDef.Thickness
	Dim oUOM As UnitsOfMeasure = oPDoc.UnitsOfMeasure
	Dim oThicknessVal As Double
	If oThkParam.Units <> "cm" Then
		oThicknessVal = oUOM.ConvertUnits(oThkParam.Value, "cm", oThkParam.Units)
	Else
		oThicknessVal = oThkParam.Value
	End If
	Dim oThickness As String = CStr(oThicknessVal)
	'start assembling the file name
	Dim oDXFFileName As String = oPN & "-" & oThickness & "-" & oMaterial & "-" & _
oComments & "-" & oProject & "-" & oVendor
	oDXFFileName = oPath & "\" & oDXFFileName & ".dxf"
	MsgBox("oDXFFileName = " & oDXFFileName,,"")
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try
			oSMDef.Unfold
			oSMDef.FlatPattern.ExitEdit
			oFP = oSMDef.FlatPattern
		Catch oEx As Exception
			Logger.Error("Unfold Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
			Exit Sub
		End Try
	End If
	ExportFlatPatternToDXF(oFP, oDXFFileName)
End Sub

Sub ExportFlatPatternToDXF(oFlatPattern As FlatPattern, oNewFullFileName As String)
	If IsNothing(oFlatPattern) Or oNewFullFileName = "" Then Exit Sub
	Dim oDataIO As DataIO = oFlatPattern.DataIO
	'<<<< FILL IN FORMAT SETTINGS THE WAY YOU WANT THEM >>>>
	Dim oFormat As String = "AcadVersion=2000"
	Try
		oDataIO.WriteDataToFile(oFormat, oNewFullFileName)
		Logger.Info("New DXF created at:" & vbCrLf & oNewFullFileName)
	Catch oEx As Exception
		Logger.Error("Export Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

  

0 Likes

Hi! 

 

I understand the miscommunication. We do use Inventor 23. I've never worked with ILogic but i think i found it. 

when i create a sheet with all the properties, it will make a flat pattern from it. But i can't find the DXF and i dont get the message "

New DXF created at; or Export Failed

 

as i can see in the messag box below, the material is written as generic, but i've choosen a material. This is a material from our own database. Does that affect the code maybe?

jwoutersJC4LN_0-1662098581901.png

 

This is the code so far; Is that correct?

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisDoc.Document
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	Dim oPath As String = System.IO.Path.GetDirectoryName(oPDoc.FullFileName)
	'gather values for use in DXF name
	Dim oProjSet As PropertySet = oPDoc.PropertySets.Item("Design Tracking Properties")
	Dim oPN As String = oProjSet.Item("Part Number").Value
	Dim oProject As String = oProjSet.Item("Project").Value
	Dim oVendor As String = oProjSet.Item("Vendor").Value
	Dim oComments As String = oPDoc.PropertySets.Item("Inventor Summary Information").Item("Comments").Value
	Dim oMaterial As String = oSMDef.ActiveSheetMetalStyle.Material.Name
	'get Thickness in 'document' units, instead of 'database' units, if they are different units
	Dim oThkParam As Inventor.Parameter = oSMDef.Thickness
	Dim oUOM As UnitsOfMeasure = oPDoc.UnitsOfMeasure
	Dim oThicknessVal As Double
	If oThkParam.Units <> "cm" Then
		oThicknessVal = oUOM.ConvertUnits(oThkParam.Value, "cm", oThkParam.Units)
	Else
		oThicknessVal = oThkParam.Value
	End If
	Dim oThickness As String = CStr(oThicknessVal)
	'start assembling the file name
	Dim oDXFFileName As String = oPN & "-" & oThickness & "-" & oMaterial & "-" & _
oComments & "-" & oProject & "-" & oVendor
	oDXFFileName = oPath & "\" & oDXFFileName & ".dxf"
	MsgBox("oDXFFileName = " & oDXFFileName,,"")
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try
			oSMDef.Unfold
			oSMDef.FlatPattern.ExitEdit
			oFP = oSMDef.FlatPattern
		Catch oEx As Exception
			Logger.Error("Unfold Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
			Exit Sub
		End Try
	End If
	ExportFlatPatternToDXF(oFP, oDXFFileName)
End Sub

Sub ExportFlatPatternToDXF(oFlatPattern As FlatPattern, oNewFullFileName As String)
	If IsNothing(oFlatPattern) Or oNewFullFileName = "" Then Exit Sub
	Dim oDataIO As DataIO = oFlatPattern.DataIO
	'<<<< FILL IN FORMAT SETTINGS THE WAY YOU WANT THEM >>>>
	Dim oFormat As String = "AcadVersion=2000"
	Try
		oDataIO.WriteDataToFile(oFormat, oNewFullFileName)
		Logger.Info("New DXF created at:" & vbCrLf & oNewFullFileName)
	Catch oEx As Exception
		Logger.Error("Export Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

  

Message 11 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.  If you are unfamilliar with iLogic, and you are not seeing any messages as feedback from running the code, but the code is not creating the DXF, I suspect that it is either not getting the FlatPattern for some reason, or you may have to familiarize yourself with how the Logger works.

It would probably be best to set your Logger to its Trace level.  When that is set, you also have the option to check the box by 'Detailed Trace'.  The detailed trace setting may be too much information to start with though, because when it is turned on it will write an entry to the Logger every time an iLogic rule is starts to run and when it stops.  There are two places where you can set the Logger level to Trace level.  One is at the very bottom of the iLogic Rule Editor dialog screen (a drop-down list with the label "Log Level" beside it).  The other place is in the iLogic Configuration dialog.  To get to that dialog, go to the Tools tab > Options panel > and click on iLogic Configuration (you may have to expand the panel down to see it).  Next, to view the iLogic Log, you must show its tab.  To do this, go to the View tab > Windows panel > click on User Interface, scroll down and check the box next to "iLogic Log", and of course check the one beside "iLogic" if you have not already.  Now those tabs will usually show up next to your Model tab (your feature history and browser nodes are under the Model tab, along the right side of your screen, by default).  Once that is showing, click on the iLogic Log tab.  If you just ran a code which uses the Logger lines of code, you will usually see those log entries on the tab.

 

However, if you do not like using that logger, because it is not visible enough, you can always just use MsgBox("your message") or MessageBox.Show("your message"), instead of the Logger.Error("your message") or Logger.Info() type lines of code.  When you use those regular messages, a message box will pop-up on your screen when the code reaches that point.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @jwoutersJC4LN.  If you are unfamilliar with iLogic, and you are not seeing any messages as feedback from running the code, but the code is not creating the DXF, I suspect that it is either not getting the FlatPattern for some reason, or you may have to familiarize yourself with how the Logger works.

It would probably be best to set your Logger to its Trace level.  When that is set, you also have the option to check the box by 'Detailed Trace'.  The detailed trace setting may be too much information to start with though, because when it is turned on it will write an entry to the Logger every time an iLogic rule is starts to run and when it stops.  There are two places where you can set the Logger level to Trace level.  One is at the very bottom of the iLogic Rule Editor dialog screen (a drop-down list with the label "Log Level" beside it).  The other place is in the iLogic Configuration dialog.  To get to that dialog, go to the Tools tab > Options panel > and click on iLogic Configuration (you may have to expand the panel down to see it).  Next, to view the iLogic Log, you must show its tab.  To do this, go to the View tab > Windows panel > click on User Interface, scroll down and check the box next to "iLogic Log", and of course check the one beside "iLogic" if you have not already.  Now those tabs will usually show up next to your Model tab (your feature history and browser nodes are under the Model tab, along the right side of your screen, by default).  Once that is showing, click on the iLogic Log tab.  If you just ran a code which uses the Logger lines of code, you will usually see those log entries on the tab.

 

However, if you do not like using that logger, because it is not visible enough, you can always just use MsgBox("your message") or MessageBox.Show("your message"), instead of the Logger.Error("your message") or Logger.Info() type lines of code.  When you use those regular messages, a message box will pop-up on your screen when the code reaches that point.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 23
jwoutersJC4LN
in reply to: WCrihfield

jwoutersJC4LN
Contributor
Contributor

Hi @WCrihfield.

 

i've found the box and set the  values to this;

jwoutersJC4LN_0-1662356738221.png

this is my browser.

 

jwoutersJC4LN_1-1662356811704.png

 

jwoutersJC4LN_2-1662356830592.png

 

if i press Run Rule, a flat pattern is made;

This is the message i get;

jwoutersJC4LN_4-1662356864961.png

 

seems like correct, but i can't find the location where the file is saved.

other question;

the name of the DXF should only be the last 4 digits of the partnumber. so in this case, 0501-8(+mm if possible?)-S235-1(+ X)-2681234-Century. So the correct name should be; 0501-8mm-S235-1x-2681234-Century.

 

hope you can help me forward:) Thanks so far 

 

Regards, Justin 

 

0 Likes

Hi @WCrihfield.

 

i've found the box and set the  values to this;

jwoutersJC4LN_0-1662356738221.png

this is my browser.

 

jwoutersJC4LN_1-1662356811704.png

 

jwoutersJC4LN_2-1662356830592.png

 

if i press Run Rule, a flat pattern is made;

This is the message i get;

jwoutersJC4LN_4-1662356864961.png

 

seems like correct, but i can't find the location where the file is saved.

other question;

the name of the DXF should only be the last 4 digits of the partnumber. so in this case, 0501-8(+mm if possible?)-S235-1(+ X)-2681234-Century. So the correct name should be; 0501-8mm-S235-1x-2681234-Century.

 

hope you can help me forward:) Thanks so far 

 

Regards, Justin 

 

Message 13 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.  It looks like the iLogic Log tab is still not showing next to your iLogic tab.  Here are a couple of screen shots of how to make it appear.  There are two ways to get it to show up.

  • WCrihfield_0-1662468927084.png
  • WCrihfield_1-1662469038864.png
  • Then you should see the iLogic Log tab like this:
  • WCrihfield_2-1662469104968.png

Once that tab is visible like this, then after you run any iLogic rules that contain 'Logger' lines of code, you can select that iLogic Log tab to see if there are any entries within it.  This is just one way to log data while a rule is running without interrupting the flow of the iLogic rule with actual pop-up messages, when it is not important enough to pause the rule for.  But you do not have to use the Logger lines of code or this iLogic Log tab, if you do not want to.  It is just a tool available to us for our convenience, and is often valuable for debugging (finding & fixing rule problems) rules.

 

As for the requested edits to the DXF file name...I think I understand, and have made some edits to the code to make them happen.  I also added in some MsgBox calls everywhere within the code where I was using Logger lines of code, to make it easier for you right now.  I also added in some extra code within the Try side of the WriteDataToFile method, that gets the directory/folder where the DXF was to be exported to, and launches a file browser window to that directory/folder.  I don't know if that will work, if there may be something wrong with the directory path that your file system doesn't like though.  Also, I noticed the file path starts with "M:\", which seems to indicate the use of a 'mapped network drive'.  We also use mapped network drives, but we have since converted most of our file paths (within settings & within codes) to use the full path back to the server computer's name, instead of using the mapped drive letter, for various reasons.  I don't know if that would help in your case or not though.  One of the reasons for this change was there were several people in our group, and some had different drive letters pointing to same location, which was causing problems when using/accessing group resources and/or shared/common code/rules.

Here is the slightly updated code:

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisDoc.Document
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	Dim oPath As String = System.IO.Path.GetDirectoryName(oPDoc.FullFileName)
	'gather values for use in DXF name
	Dim oProjSet As PropertySet = oPDoc.PropertySets.Item("Design Tracking Properties")
	Dim oPN As String = oProjSet.Item("Part Number").Value
	'If oPN is greater than 4 characters, shorten to only last 4 characters
	If oPN.Length > 4 Then 
		oPN = Right(oPN, 4)
	End If
	Dim oProject As String = oProjSet.Item("Project").Value
	Dim oVendor As String = oProjSet.Item("Vendor").Value
	Dim oComments As String = oPDoc.PropertySets.Item("Inventor Summary Information").Item("Comments").Value
	oComments = oComments & "x"
	Dim oMaterial As String = oSMDef.ActiveSheetMetalStyle.Material.Name
	'get Thickness in 'document' units, instead of 'database' units, if they are different units
	Dim oThkParam As Inventor.Parameter = oSMDef.Thickness
	Dim oUOM As UnitsOfMeasure = oPDoc.UnitsOfMeasure
	Dim oThicknessVal As Double
	If oThkParam.Units <> "cm" Then
		oThicknessVal = oUOM.ConvertUnits(oThkParam.Value, "cm", oThkParam.Units)
	Else
		oThicknessVal = oThkParam.Value
	End If
	Dim oThickness As String = CStr(oThicknessVal) & "mm"
	'assemble file name
	Dim oDXFFileName As String = oPN & "-" & oThickness & "-" & oMaterial & "-" & _
oComments & "-" & oProject & "-" & oVendor
	oDXFFileName = oPath & "\" & oDXFFileName & ".dxf"
	MsgBox("oDXFFileName = " & oDXFFileName,,"")
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try
			oSMDef.Unfold
			oSMDef.FlatPattern.ExitEdit
			oFP = oSMDef.FlatPattern
		Catch oEx As Exception
			MsgBox("Unfold Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "UnFold Failed")
			Logger.Error("Unfold Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
			Exit Sub
		End Try
	End If
	ExportFlatPatternToDXF(oFP, oDXFFileName)
End Sub

Sub ExportFlatPatternToDXF(oFlatPattern As FlatPattern, oNewFullFileName As String)
	If IsNothing(oFlatPattern) Or oNewFullFileName = "" Then Exit Sub
	Dim oDataIO As DataIO = oFlatPattern.DataIO
	'<<<< FILL IN FORMAT SETTINGS THE WAY YOU WANT THEM >>>>
	Dim oFormat As String = "AcadVersion=2000"
	Try
		oDataIO.WriteDataToFile(oFormat, oNewFullFileName)
		MsgBox("New DXF created at:" & vbCrLf & oNewFullFileName, vbInformation, "DXF Created")
		Logger.Info("New DXF created at:" & vbCrLf & oNewFullFileName)
		'get directory/folder where DXF was saved to
		Dim oDirectory As String = System.IO.Path.GetDirectoryName(oNewFullFileName)
		'try to open that Directory/Folder in a file browser window
		ThisDoc.Launch(oDirectory)
	Catch oEx As Exception
		MsgBox("Export Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "DXF Export Failed")
		Logger.Error("Export Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @jwoutersJC4LN.  It looks like the iLogic Log tab is still not showing next to your iLogic tab.  Here are a couple of screen shots of how to make it appear.  There are two ways to get it to show up.

  • WCrihfield_0-1662468927084.png
  • WCrihfield_1-1662469038864.png
  • Then you should see the iLogic Log tab like this:
  • WCrihfield_2-1662469104968.png

Once that tab is visible like this, then after you run any iLogic rules that contain 'Logger' lines of code, you can select that iLogic Log tab to see if there are any entries within it.  This is just one way to log data while a rule is running without interrupting the flow of the iLogic rule with actual pop-up messages, when it is not important enough to pause the rule for.  But you do not have to use the Logger lines of code or this iLogic Log tab, if you do not want to.  It is just a tool available to us for our convenience, and is often valuable for debugging (finding & fixing rule problems) rules.

 

As for the requested edits to the DXF file name...I think I understand, and have made some edits to the code to make them happen.  I also added in some MsgBox calls everywhere within the code where I was using Logger lines of code, to make it easier for you right now.  I also added in some extra code within the Try side of the WriteDataToFile method, that gets the directory/folder where the DXF was to be exported to, and launches a file browser window to that directory/folder.  I don't know if that will work, if there may be something wrong with the directory path that your file system doesn't like though.  Also, I noticed the file path starts with "M:\", which seems to indicate the use of a 'mapped network drive'.  We also use mapped network drives, but we have since converted most of our file paths (within settings & within codes) to use the full path back to the server computer's name, instead of using the mapped drive letter, for various reasons.  I don't know if that would help in your case or not though.  One of the reasons for this change was there were several people in our group, and some had different drive letters pointing to same location, which was causing problems when using/accessing group resources and/or shared/common code/rules.

Here is the slightly updated code:

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisDoc.Document
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	Dim oPath As String = System.IO.Path.GetDirectoryName(oPDoc.FullFileName)
	'gather values for use in DXF name
	Dim oProjSet As PropertySet = oPDoc.PropertySets.Item("Design Tracking Properties")
	Dim oPN As String = oProjSet.Item("Part Number").Value
	'If oPN is greater than 4 characters, shorten to only last 4 characters
	If oPN.Length > 4 Then 
		oPN = Right(oPN, 4)
	End If
	Dim oProject As String = oProjSet.Item("Project").Value
	Dim oVendor As String = oProjSet.Item("Vendor").Value
	Dim oComments As String = oPDoc.PropertySets.Item("Inventor Summary Information").Item("Comments").Value
	oComments = oComments & "x"
	Dim oMaterial As String = oSMDef.ActiveSheetMetalStyle.Material.Name
	'get Thickness in 'document' units, instead of 'database' units, if they are different units
	Dim oThkParam As Inventor.Parameter = oSMDef.Thickness
	Dim oUOM As UnitsOfMeasure = oPDoc.UnitsOfMeasure
	Dim oThicknessVal As Double
	If oThkParam.Units <> "cm" Then
		oThicknessVal = oUOM.ConvertUnits(oThkParam.Value, "cm", oThkParam.Units)
	Else
		oThicknessVal = oThkParam.Value
	End If
	Dim oThickness As String = CStr(oThicknessVal) & "mm"
	'assemble file name
	Dim oDXFFileName As String = oPN & "-" & oThickness & "-" & oMaterial & "-" & _
oComments & "-" & oProject & "-" & oVendor
	oDXFFileName = oPath & "\" & oDXFFileName & ".dxf"
	MsgBox("oDXFFileName = " & oDXFFileName,,"")
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try
			oSMDef.Unfold
			oSMDef.FlatPattern.ExitEdit
			oFP = oSMDef.FlatPattern
		Catch oEx As Exception
			MsgBox("Unfold Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "UnFold Failed")
			Logger.Error("Unfold Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
			Exit Sub
		End Try
	End If
	ExportFlatPatternToDXF(oFP, oDXFFileName)
End Sub

Sub ExportFlatPatternToDXF(oFlatPattern As FlatPattern, oNewFullFileName As String)
	If IsNothing(oFlatPattern) Or oNewFullFileName = "" Then Exit Sub
	Dim oDataIO As DataIO = oFlatPattern.DataIO
	'<<<< FILL IN FORMAT SETTINGS THE WAY YOU WANT THEM >>>>
	Dim oFormat As String = "AcadVersion=2000"
	Try
		oDataIO.WriteDataToFile(oFormat, oNewFullFileName)
		MsgBox("New DXF created at:" & vbCrLf & oNewFullFileName, vbInformation, "DXF Created")
		Logger.Info("New DXF created at:" & vbCrLf & oNewFullFileName)
		'get directory/folder where DXF was saved to
		Dim oDirectory As String = System.IO.Path.GetDirectoryName(oNewFullFileName)
		'try to open that Directory/Folder in a file browser window
		ThisDoc.Launch(oDirectory)
	Catch oEx As Exception
		MsgBox("Export Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "DXF Export Failed")
		Logger.Error("Export Failed:" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 23
jwoutersJC4LN
in reply to: WCrihfield

jwoutersJC4LN
Contributor
Contributor

Morning,

 

i've tried the code but this is what i have in the I Logic Log;

All properties are filled in and correct so i dont know what happend.. 

jwoutersJC4LN_1-1662628528344.png

the example when i press Run Rule  is correct, instead of the "material". This is not "Generic"..

But it failed with saving i think?

 

It will create a flat pattern but still not save in the correct folder.

"M" is our server where the parts are stored..

 

See your response!

Regards, Justin 

 

jwoutersJC4LN_0-1662628206231.png

 

0 Likes

Morning,

 

i've tried the code but this is what i have in the I Logic Log;

All properties are filled in and correct so i dont know what happend.. 

jwoutersJC4LN_1-1662628528344.png

the example when i press Run Rule  is correct, instead of the "material". This is not "Generic"..

But it failed with saving i think?

 

It will create a flat pattern but still not save in the correct folder.

"M" is our server where the parts are stored..

 

See your response!

Regards, Justin 

 

jwoutersJC4LN_0-1662628206231.png

 

Message 15 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.  I can see from the Logger entry's error data that it is encoutering an error at the line of code where it is using 'WriteDataToFile'.  I am not 100% sure why though.

Try changing this line:

Dim oFormat As String = "AcadVersion=2000"

...to this:

Dim oFormat As String = "FLAT PATTERN DXF?AcadVersion=2000"

...and see if that makes an difference.

Also, as for the material's name not being correct...I think maybe we need to get its name from a different source.

Try changing this line of code:

Dim oMaterial As String = oSMDef.ActiveSheetMetalStyle.Material.Name

...to this:

Dim oMaterial As String = oPDoc.ActiveMaterial.DisplayName

...and see if that gets the proper material name for you.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Hi @jwoutersJC4LN.  I can see from the Logger entry's error data that it is encoutering an error at the line of code where it is using 'WriteDataToFile'.  I am not 100% sure why though.

Try changing this line:

Dim oFormat As String = "AcadVersion=2000"

...to this:

Dim oFormat As String = "FLAT PATTERN DXF?AcadVersion=2000"

...and see if that makes an difference.

Also, as for the material's name not being correct...I think maybe we need to get its name from a different source.

Try changing this line of code:

Dim oMaterial As String = oSMDef.ActiveSheetMetalStyle.Material.Name

...to this:

Dim oMaterial As String = oPDoc.ActiveMaterial.DisplayName

...and see if that gets the proper material name for you.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 16 of 23
jwoutersJC4LN
in reply to: WCrihfield

jwoutersJC4LN
Contributor
Contributor

Hi @WCrihfield 

This is great ๐Ÿ˜€

 

Works as we want!

One isue;

We do have specific layers to generate a DXF. 

Only the inner and outer profile are vissible. Is this also possible to create? 

Or maybe a reference to our Configuration?

 

This is the file and location where the configuration is saved;

M:\Inventor\Inventor instellingen\PVE-Snijsjablonen_2023

 

jwoutersJC4LN_0-1662712777961.png

 

 

Kind regards,

 

Justin 

0 Likes

Hi @WCrihfield 

This is great ๐Ÿ˜€

 

Works as we want!

One isue;

We do have specific layers to generate a DXF. 

Only the inner and outer profile are vissible. Is this also possible to create? 

Or maybe a reference to our Configuration?

 

This is the file and location where the configuration is saved;

M:\Inventor\Inventor instellingen\PVE-Snijsjablonen_2023

 

jwoutersJC4LN_0-1662712777961.png

 

 

Kind regards,

 

Justin 

Message 17 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.  As far as I know, there are two routes to take when exporting sheet metal flat pattern's to DXF by code.  One main route is to use the specific TranslatorAddIn for exporting DXF's, then specify the file location of that configuration file as the option, but I usually only export drawings to DXF that way, not sheet metal flat patterns.  The other route is to use the FlatPattern's own DataIO object, and its WriteDataToFile method.  When using the WriteDataToFile method, you can individually specify a ton of settings right in the code itself, by entering more data into the oFormat variable's String value.  The list of possible settings are documented at the following link:

https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=DataIO_WriteDataToFile 

'or https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-54B44291-4E1D-4CDB-80CB-FBB38D0239C2

 

When using the TranslatorAddIn route, you can specify the full file name of the configuration file as the one main option.  Then it simply refers to that file for the settings.  To use this route, you find/get the TranslatorAddIn object designed for exporting DXF's, then create the needed variables, then specify the configuration file's location as the value of one of its options.  Then use the TranslatorAddIn .SaveCopyAs method, with those variables as input, to export the DXF.  I'm sure there are examples of this type of code around this forum, but the only examples I have of this are for exporting an IDW type drawing out as a DXF using this route.

Here is an example of a seperate Sub routine you can use for exporting a DrawingDocument out as a DXF using the TranslatorAddIn route.  You would have to have a Sub Main...End Sub code at the top of your rule though, then call this Sub to run within your Sub Main area somewhere to try it out.  As I said, I don't think I have tried using a FlatPattern (or similar object) as input for this route yet, but you can try.

Sub ExportIDWtoDXF(oDrawing As DrawingDocument, oNewFullFileName As String)
	Dim oDXF As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById( _
	"{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
	If IsNothing(oDXF) Then
		MsgBox("DXF Translator Add-in not found.  Exiting.", vbCritical, "")
		'Logger.Debug("DXF Translator Add-in not found.")
		Exit Sub
	End If
	
	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium
	
	If System.IO.File.Exists(oNewFileName) Then
		oAns = MsgBox("A DXF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?", vbYesNo + vbQuestion + vbDefaultButton2, "DXF FILE EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = oNewFileName
	'<<<< CHANGE THIS IF NEEDED >>>>
	Dim oINI_File As String = "C:\Users\Public\Documents\Autodesk\Inventor 2022\Design Data\DWG-DXF\exportdxf.ini"
	If Not System.IO.File.Exists(oINI_File) Then
		MsgBox("Couldn't find this INI file:  " & oINI_File & ".  Exiting.", vbExclamation, "")
		Exit Sub
	End If
	If oDXF.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
		oOptions.Value("Export_Acad_IniFile") = oINI_File
	End If
	Try
		oDXF.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
	Catch oEx As Exception
		MsgBox("SaveCopyAs DXF failed." & vbCrLf & _
		oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "")
		'Logger.Error(oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @jwoutersJC4LN.  As far as I know, there are two routes to take when exporting sheet metal flat pattern's to DXF by code.  One main route is to use the specific TranslatorAddIn for exporting DXF's, then specify the file location of that configuration file as the option, but I usually only export drawings to DXF that way, not sheet metal flat patterns.  The other route is to use the FlatPattern's own DataIO object, and its WriteDataToFile method.  When using the WriteDataToFile method, you can individually specify a ton of settings right in the code itself, by entering more data into the oFormat variable's String value.  The list of possible settings are documented at the following link:

https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=DataIO_WriteDataToFile 

'or https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-54B44291-4E1D-4CDB-80CB-FBB38D0239C2

 

When using the TranslatorAddIn route, you can specify the full file name of the configuration file as the one main option.  Then it simply refers to that file for the settings.  To use this route, you find/get the TranslatorAddIn object designed for exporting DXF's, then create the needed variables, then specify the configuration file's location as the value of one of its options.  Then use the TranslatorAddIn .SaveCopyAs method, with those variables as input, to export the DXF.  I'm sure there are examples of this type of code around this forum, but the only examples I have of this are for exporting an IDW type drawing out as a DXF using this route.

Here is an example of a seperate Sub routine you can use for exporting a DrawingDocument out as a DXF using the TranslatorAddIn route.  You would have to have a Sub Main...End Sub code at the top of your rule though, then call this Sub to run within your Sub Main area somewhere to try it out.  As I said, I don't think I have tried using a FlatPattern (or similar object) as input for this route yet, but you can try.

Sub ExportIDWtoDXF(oDrawing As DrawingDocument, oNewFullFileName As String)
	Dim oDXF As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById( _
	"{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
	If IsNothing(oDXF) Then
		MsgBox("DXF Translator Add-in not found.  Exiting.", vbCritical, "")
		'Logger.Debug("DXF Translator Add-in not found.")
		Exit Sub
	End If
	
	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium
	
	If System.IO.File.Exists(oNewFileName) Then
		oAns = MsgBox("A DXF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?", vbYesNo + vbQuestion + vbDefaultButton2, "DXF FILE EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = oNewFileName
	'<<<< CHANGE THIS IF NEEDED >>>>
	Dim oINI_File As String = "C:\Users\Public\Documents\Autodesk\Inventor 2022\Design Data\DWG-DXF\exportdxf.ini"
	If Not System.IO.File.Exists(oINI_File) Then
		MsgBox("Couldn't find this INI file:  " & oINI_File & ".  Exiting.", vbExclamation, "")
		Exit Sub
	End If
	If oDXF.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
		oOptions.Value("Export_Acad_IniFile") = oINI_File
	End If
	Try
		oDXF.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
	Catch oEx As Exception
		MsgBox("SaveCopyAs DXF failed." & vbCrLf & _
		oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "")
		'Logger.Error(oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 18 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

There is also a way, by code, to specify that you would like certain specific geometry to be on 'custom' layers when they get exported to DXF, before using that manual export process, but I honestly have not used it before in my own work environment yet.  If you are interested in learning more about that process/idea, you can refer to @JelteDeJong 's blog post about the topic (link below).

http://www.hjalte.nl/18-setting-extra-layers-in-dxf-exports 

I am actually currently attempting to help someone else out with a similar request, which is attempting to make use of this technique.  Below is a link to that other post too if you are interested.

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/i-want-it-find-the-edge-form-the-fea... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

There is also a way, by code, to specify that you would like certain specific geometry to be on 'custom' layers when they get exported to DXF, before using that manual export process, but I honestly have not used it before in my own work environment yet.  If you are interested in learning more about that process/idea, you can refer to @JelteDeJong 's blog post about the topic (link below).

http://www.hjalte.nl/18-setting-extra-layers-in-dxf-exports 

I am actually currently attempting to help someone else out with a similar request, which is attempting to make use of this technique.  Below is a link to that other post too if you are interested.

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/i-want-it-find-the-edge-form-the-fea... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 19 of 23
jwoutersJC4LN
in reply to: WCrihfield

jwoutersJC4LN
Contributor
Contributor

Hi @WCrihfield ,

 

As i said, i'm totaly not a programmer so it's to hard for me to change the code for a DXF. I've tried to change some settings in the "DesignData, DWG-DXF folder, but that's not working so far.. 

 

 

I've tried to delete all the layers, exept the inner/outer profile in the "FlatPattern.xml" document. But this is also "code" and not working.. Can you help me maybe with this? 

 

Thanks in advance!

 

Regards,

 

Justin

0 Likes

Hi @WCrihfield ,

 

As i said, i'm totaly not a programmer so it's to hard for me to change the code for a DXF. I've tried to change some settings in the "DesignData, DWG-DXF folder, but that's not working so far.. 

 

 

I've tried to delete all the layers, exept the inner/outer profile in the "FlatPattern.xml" document. But this is also "code" and not working.. Can you help me maybe with this? 

 

Thanks in advance!

 

Regards,

 

Justin

Message 20 of 23
WCrihfield
in reply to: jwoutersJC4LN

WCrihfield
Mentor
Mentor

Hi @jwoutersJC4LN.  You said earlier that you have specific layer requirements in your exported DXF's, and the current code is not meeting all your needs just yet, correct?  Can you please explain in detail how you need everything set up?  If we stick with using the WriteDataToFile method that I am familiar with, then we can't just specify the a configuration file for it to use.  We have to tell it how we want all those settings to be set within the code.  I can help you with the code part, but I will need to know exactly how you need all those settings to be set, so that I can put those preferences into the code.  What do you want the layers to be named, and what geometry should be on those layers.  When you look at your user interface dialog options for exporting a flat pattern to DXF, which way do you need each of those settings set?  It shows all the default layers, and their default names, and what geometry will be put on those layers by default, but we can change the names of those layers if you want.  Just let me know, and I will try to update the code as needed, when I get time to do so.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @jwoutersJC4LN.  You said earlier that you have specific layer requirements in your exported DXF's, and the current code is not meeting all your needs just yet, correct?  Can you please explain in detail how you need everything set up?  If we stick with using the WriteDataToFile method that I am familiar with, then we can't just specify the a configuration file for it to use.  We have to tell it how we want all those settings to be set within the code.  I can help you with the code part, but I will need to know exactly how you need all those settings to be set, so that I can put those preferences into the code.  What do you want the layers to be named, and what geometry should be on those layers.  When you look at your user interface dialog options for exporting a flat pattern to DXF, which way do you need each of those settings set?  It shows all the default layers, and their default names, and what geometry will be put on those layers by default, but we can change the names of those layers if you want.  Just let me know, and I will try to update the code as needed, when I get time to do so.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report