I-Logic to catch perimeter and pierces

I-Logic to catch perimeter and pierces

didin.suryadi6JREK
Advocate Advocate
580 Views
6 Replies
Message 1 of 7

I-Logic to catch perimeter and pierces

didin.suryadi6JREK
Advocate
Advocate

Hi,

 

I have the attached I-logic to convert all sheet metal parts to DXF in one folder.

All works fine, But I want to add some code that lets the logic catch some information from the sheet metal parts:

- Innerperimeters 

- Outerperimeters

- Pierces

- Total Perimeter

didinsuryadi6JREK_0-1674029330190.png

 

Can anyone check and give a solution?

 

0 Likes
581 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @didin.suryadi6JREK.  The information you are asking for can be a little complex to gather, and needs a pretty decent amount of code to extract, so I suggest that you use a separate routine to gather that information to the custom iProperties before running your other rule to export the flat pattern to DXF.  That will help keep the main functionalities of both codes shorter and easier to read, and understand.  If the export code needs to use those numbers for something, it can simply get those values from the custom iProperties.

Here is an iLogic rule you could try out for extracting the data from the sheet metal flat pattern, and writing it to custom iProperties.  The 'StandardObjectFactory' tool I am using near the end is only available in 2021 or later versions of Inventor, I believe, so if you have an earlier version, we may need to change that part a bit.

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 oPDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Exit Sub
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try : oSMDef.Unfold : Catch : Exit Sub : End Try
		oFP = oSMDef.FlatPattern
		oFP.ExitEdit
	End If
	If IsNothing(oFP) Then Exit Sub
	Dim oBaceFace As Face = oFP.BaseFace
	Dim oOuterPerimeter, oInnerPerimeters, oTotalPerimeter As Double
	Dim oPierces As Integer = 0
	For Each oEL As EdgeLoop In oBaceFace.EdgeLoops
		Dim oLength As Double = 0.0
		Dim oTotalLength As Double = 0.0
		If oEL.Edges.Count = 1 Then
			oEL.Edges.Item(1).Evaluator.GetLengthAtParam(0.0, 1.0, oTotalLength)
		Else
			For Each oEdge As Edge In oEL.Edges
				oEdge.Evaluator.GetLengthAtParam(0.0, 1.0, oLength)
				oTotalLength = oTotalLength + oLength
			Next 'oEdge
		End If
		If oEL.IsOuterEdgeLoop Then
			oOuterPerimeter = oTotalLength
			oPierces = oPierces + 1
		Else
			oInnerPerimeters = oInnerPerimeters + oTotalLength
			oPierces = oPierces + 1
		End If
	Next 'oEL
	oTotalPerimeter = oOuterPerimeter + oInnerPerimeters
	SOP = StandardObjectFactory.Create(oPDoc)
	SOP.iProperties.Value("Custom", "OuterPerimeter") = oOuterPerimeter
	SOP.iProperties.Value("Custom", "InnerPerimeters") = oInnerPerimeters
	SOP.iProperties.Value("Custom", "TotalPerimeter") = oTotalPerimeter
	SOP.iProperties.Value("Custom", "Pierces") = oPierces
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
Message 3 of 7

didin.suryadi6JREK
Advocate
Advocate

Hi @WCrihfield 

 

Thanks. I am using 2019 version. The code you sent is not working on my version.

didinsuryadi6JREK_0-1674092677134.png

 

Can you give the code that complies with the 2019 version?

Is there any chance to combine the code with the i-logic i sent on the first post?

 

Rgds

 

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

Hi @didin.suryadi6JREK.  Been really busy at work today, so I didn't have much time to work on this, or respond much.  Here is something you can try:

Sub Main
	If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("WARNING!, RULE INI HANYA BERLAKU UNTUK FILE 3D ASSEMBLY.", "iLogic - CONVERT SHEET METAL TO DXF's")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oADoc.FullFileName)
	RUsure = MessageBox.Show ("This Rule is to create DXF files from sheet metal" _
		& vbLf & "PASTIKAN SEMUA PART SHEET METAL SUDAH BENAR & DI SAVE" _
		& vbLf & " " _
		& vbLf & "Are you sure you want to create DXF for all of the assembly components?" _
		& vbLf & "This could take a while.", "iLogic - CONVERT SHEET METAL TO DXF's",MessageBoxButtons.YesNo)
	If RUsure = vbNo Then 	Exit Sub
	Dim oPath As String = System.IO.Path.GetDirectoryName(oADoc.FullFileName)
	'get DXF target folder path
	Dim oFolder As String = oPath & "\" & oAsmName & " .DXF Files"
	If Not System.IO.Directory.Exists(oFolder) Then System.IO.Directory.CreateDirectory(oFolder)
	Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	For Each oRefDoc As Document In oRefDocs
		'if it is not a Sheet Metal Part, skip to next oRefDoc
		If oRefDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
		Dim oPDoc As PartDocument = oRefDoc
		If oPDoc.FileSaveCounter = 0 Then Continue For 'or maybe try saving it first, then try to use it
		If Not oPDoc.Open Then oPDoc = ThisApplication.Documents.Open(oPDoc.FullDocumentName, True)
		Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oPDoc.FullFileName)
		Dim CustomName As String = " "
		Try : CustomName = iProperties.Value(oPDoc.DisplayName, "Custom", "CustomName") : Catch : End Try
		'Set the DXF target file name
		Dim oDXFFullFileName As String = oFolder & "\" & CustomName & " " & oFileName & ".dxf"
		Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
		Dim oFP As FlatPattern = Nothing
		If oSMDef.HasFlatPattern Then
			oFP = oSMDef.FlatPattern
		Else
			Try
				oSMDef.Unfold
				oFP = oSMDef.FlatPattern
				oFP.ExitEdit
			Catch : End Try
		End If
		If IsNothing(oFP) Then : oPDoc.Close : Continue For : End If
		WritePerimeterAndPierceDataToProperties(oFP)
		Dim sOut As String
		sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=Piece&InteriorProfilesLayer=Hole&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_ARC_CENTERS;IV_TOOL_CENTER;IV_TOOL_CENTER_DOWN"
		Try : oFP.DataIO.WriteDataToFile(sOut, oDXFFullFileName) : Catch : End Try
		oPDoc.Close
	Next
	MessageBox.Show("Conversion is done! Please check the folder!","Sheet metal to DXF's ", MessageBoxButtons.OK)
End Sub

Sub WritePerimeterAndPierceDataToProperties(oFP As FlatPattern)
	If IsNothing(oFP) Then Exit Sub
	Dim oPDoc As PartDocument = oFP.Parent.Document
	Dim oBaceFace As Face = oFP.BaseFace
	Dim oOuterPerimeter, oInnerPerimeters, oTotalPerimeter As Double
	Dim oPierces As Integer = 0
	For Each oEL As EdgeLoop In oBaceFace.EdgeLoops
		Dim oLength As Double = 0.0
		Dim oTotalLength As Double = 0.0
		If oEL.Edges.Count = 1 Then
			oEL.Edges.Item(1).Evaluator.GetLengthAtParam(0.0, 1.0, oTotalLength)
		Else
			For Each oEdge As Edge In oEL.Edges
				oEdge.Evaluator.GetLengthAtParam(0.0, 1.0, oLength)
				oTotalLength = oTotalLength + oLength
			Next 'oEdge
		End If
		If oEL.IsOuterEdgeLoop Then
			oOuterPerimeter = oTotalLength
			oPierces = oPierces + 1
		Else
			oInnerPerimeters = oInnerPerimeters + oTotalLength
			oPierces = oPierces + 1
		End If
	Next 'oEL
	oTotalPerimeter = oOuterPerimeter + oInnerPerimeters
	Dim oCProps As PropertySet = oPDoc.PropertySets.Item("Inventor User Defined Properties")
	Try
		oCProps.Item("OuterPerimeter").Value = oOuterPerimeter
		oCProps.Item("InnerPerimeters").Value = oInnerPerimeters
		oCProps.Item("TotalPerimeter").Value = oTotalPerimeter
		oCProps.Item("Pierces").Value = oPierces
	Catch : End Try
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 7

didin.suryadi6JREK
Advocate
Advocate

Hi @WCrihfield 


Thank you for the codes. I ran it and it worked but it looks like the result is not what I wanted.

Innerperimeters, Outerperimeters should be the length of the cutting line that we require to calculate for laser cutting time. Sorry if it wasn't clear before.

In the below example 3d part the perimeters would be:

- InnerPerimeters = 4.72 in

- OuterPerimeter = 15.75 in

- TotalPerimeter = 20.47 in

 

didinsuryadi6JREK_0-1674180338678.png

 

When I ran your codes the perimeters are not the same, They are 4,4 and the total is 8 See below after I ran it.

didinsuryadi6JREK_1-1674180474504.png

 

I also attached the assembly file just in case you want to open and ran the code.

 

Thanks in advance.

 

Regards

 

Didin

 

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor

Hi @didin.suryadi6JREK.  I see that I forgot a step while using the Edge.Evaluator tools (CurveEvaluator).  I needed to use the Evaluator.GetParamExtents first, to get the Min & Max params, then use those instead of zero and one for the first two input params of the Evaluator.GetLengthAtParam method, to get the true length of each Edge.  I was assuming that zero and one represented a percentage of the edges overall length, as the 'from & to' input parameters, and that the method would understand that I want 100% of its length,  but I was apparently not remembering it correctly.

To fix the code, I replaced these these two lines:

oEL.Edges.Item(1).Evaluator.GetLengthAtParam(0.0, 1.0, oTotalLength)
oEdge.Evaluator.GetLengthAtParam(0.0, 1.0, oLength)

...with these blocks of code, in the same order:

Dim oMin, oMax As Double
oEL.Edges.Item(1).Evaluator.GetParamExtents(oMin, oMax)
oEL.Edges.Item(1).Evaluator.GetLengthAtParam(oMin, oMax, oTotalLength)
Dim oMin, oMax As Double
oEdge.Evaluator.GetParamExtents(oMin, oMax)
oEdge.Evaluator.GetLengthAtParam(oMin, oMax, oLength)

But also keep in mind that the values returned may be in 'database units' (centimeters), instead of inches.  If that is the case, then you may need to convert the values from centimeters to inches.  I have included some lines of code for these units conversions in the updated code attached in the text file below.  Give this version a try, and let me know if it is working OK for you or not.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 7

didin.suryadi6JREK
Advocate
Advocate

Hi @WCrihfield 

 

Thank You.

I have tried to use the code you sent. But I don't know y the perimeters didn't come up.

Also, I tried to skip the conversion units code and this came up with no results. The perimeters were empty.

 

didinsuryadi6JREK_0-1674445559470.png

I attached the assembly file that i used for trial.

 

Please Advise.

 

RGds

 

 

0 Likes