iLogic: Scan for sheet metal parts and flatten

iLogic: Scan for sheet metal parts and flatten

Carl_mgfx
Advocate Advocate
4,039 Views
16 Replies
Message 1 of 17

iLogic: Scan for sheet metal parts and flatten

Carl_mgfx
Advocate
Advocate

Good day forum people 

 

I am trying to put together some code that will scan an assembly (first level of parts is ok) and create flat patterns in any sheet metal parts.  I have a bit of code I've pieced together from a few places but I can't seem to get it to work.  I'm also not 100% sure what the error is that I'm getting.  I would like for the code to be run at assembly level to keep it simple for the user.  Any help will be appreciated

 

 

oCompDef = ThisDoc.Document.ComponentDefinition
oAssyOccurrences = oCompDef.Occurrences

Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAssyOccurrences
	If oOccurrence.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
		Dim partDoc As Document
		Dim SMCompDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition
			If SMCompDef.HasFlatPattern = False Then
       			SMCompDef.Unfold
			Else
       			SMCompDef.FlatPattern.Edit
			End If
		SMCompDef.FlatPattern.ExitEdit
		partDoc.Close(False)
	End If
Next

iLogicVb.UpdateWhenDone = True

 


Kind Regards
Carl van Rooyen
Application Engineer

Inventor 2019, Vault 2019,
HP ZBook 15u G4, Win 10 64Bit (ver 1709), i7-7500U, 16 GB Ram
Intel HD Graphics 620 with AMD Firepro W4190M
0 Likes
Accepted solutions (1)
4,040 Views
16 Replies
Replies (16)
Message 2 of 17

philip1009
Advisor
Advisor

I have a few questions before I change the code I have for flat pattern extracting.

 

1. Do you have Product Design and Manufacturing Collection 2019?  If you have Inventor 2019 as part of a software collection from your company, then you have access to the new Nesting Utility which is designed for this purpose.  Here's a video detailing how it can be used:  https://www.youtube.com/watch?v=8yhklj9zfPs&t=1490s

 

2.  If you don't have access to the Nesting Utility, I'll need to know where you want the flat patterns extracted to, i.e. the same folder as the part files or in a specific folder elsewhere on your local drive or network drive.

 

3.  What settings are you using for manual flat pattern export such as dxf or dwg, layer, and geometry settings?  Each of those settings can be represented in code.

 

EDIT:  4.  One question I forgot to ask originally, do you use Autodesk Vault?

0 Likes
Message 3 of 17

AlexFielder
Advisor
Advisor

You should check each partdocument is actually a sheet metal component like this:

 

Public Sub ExportFlatPatternDXF()
	If Not ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		Exit Sub
	Else If Not TypeOf ThisApplication.ActiveDocument.ComponentDefinition Is SheetMetalComponentDefinition Then
		Exit Sub
	End If
	
	Dim oDoc As PartDocument
    oDoc = ThisApplication.ActiveDocument
    
	Dim oCompDef As SheetMetalComponentDefinition
    oCompDef = oDoc.ComponentDefinition
    If oCompDef.HasFlatPattern = False Then
	    oCompDef.Unfold
    Else
	    oCompDef.FlatPattern.Edit
    End If

    Dim sOut As String
    sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_INTERIOR_PROFILES"

    Dim sFname As String = NewPath & System.IO.Path.GetFileNameWithoutExtension(ThisApplication.activedocument.fulldocumentname) & ".dxf"
    'sFname = ThisDoc.Path & "\" & ThisDoc.FileName(False) & ".dxf"

    MessageBox.Show("DXF SAVED TO: " & sFname ,"DXF Saved", MessageBoxButtons.OK)
    oCompDef.DataIO.WriteDataToFile( sOut, sFname)
	oDoc = ThisApplication.ActiveDocument

	Dim oSMDef As SheetMetalComponentDefinition
	oSMDef = oDoc.ComponentDefinition
	oSMDef.FlatPattern.ExitEdit
End Sub

You'll also get better performance if you check each document instead of the occurrences like this:

 

 

Sub Main()
	oAdoc = ThisApplication.ActiveDocument
	oAcompdef = oAdoc.ComponentDefinition

	'conversion factor cm to in
	oCF = 10
	If oAdoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		For Each oDoc As Document In oAdoc.AllReferencedDocuments
			If Not oDoc.FullFileName.Contains("Content Center") Then 'not CC file
				If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
					Dim PartDoc As PartDocument = oDoc
'do your stuff here.
				Else
					 
				End If
			End If
		Next oDoc
	End If
End Sub

 

Combining these two approaches should solve your issue. 🙂

Message 4 of 17

Carl_mgfx
Advocate
Advocate

@philip1009 and @AlexFielder

 

Thanks for the replies, I will see what I can with this.  Although all I am looking at doing is ensuring that the sheet metal parts have flat patterns, I do not need to export a DXF file.

 

I am looking at inserting this code into assemblies to make sure all sheet metal parts have flat patterns so that there are no problems loading the assembly into the Nesting Utility.


Kind Regards
Carl van Rooyen
Application Engineer

Inventor 2019, Vault 2019,
HP ZBook 15u G4, Win 10 64Bit (ver 1709), i7-7500U, 16 GB Ram
Intel HD Graphics 620 with AMD Firepro W4190M
0 Likes
Message 5 of 17

philip1009
Advisor
Advisor

Okay, that makes sense now.  You just need to go through your sheet metal parts, if a flat pattern ins't made, attempt to flatten it, if an error occurs add that part to a list, then make a list of sheet metal parts that need to be fixed before being flattened.  I'm surprised this kind of error checking isn't a function in the Nesting Utility, I haven't had a chance to use it yet.

 

I'll see if I can come up with something in my spare time unless someone already has a response.

0 Likes
Message 6 of 17

Carl_mgfx
Advocate
Advocate

@philip1009 Thanks!

 

I've been trying to piece together code and pulling my hair out because I'm unfortunately just not getting it right.


Kind Regards
Carl van Rooyen
Application Engineer

Inventor 2019, Vault 2019,
HP ZBook 15u G4, Win 10 64Bit (ver 1709), i7-7500U, 16 GB Ram
Intel HD Graphics 620 with AMD Firepro W4190M
0 Likes
Message 7 of 17

philip1009
Advisor
Advisor
Accepted solution

I've got something that's working for me so far, but everything can't be accounted for.  Sorry there aren't any comments, I'll just explain it the best I can here.

 

For the first loop, it's perusing a list of all documents used to build the assembly.  If a sheet metal part is found, it then looks to see if a Flat Pattern exists in the part, the nice thing is you can check for a flat pattern without opening the file directly since it's being referenced already in the assembly.

 

If the Flat Pattern does exist, the loop moves on to the next document.  If it doesn't exist it will open the file.  Then the code will Try to 1. create the flat pattern, 2. exit the flat pattern, and 3. save the file.  If an error occurs on any of those steps, it will be added to the Error List.  In any case, if an attempt is successful or not, the document will close, that way you won't be left with a mess of open files at the end.

 

If any errors were found, the code will then create a text file in the same folder location as the top assembly and list all of the documents that generated errors, then it will open the text file for you.  If a previous text file of the same name already exists in the folder, it will overwrite that file.

 

The message box saying the process is completed is the end of the code, if a text file wasn't generated, that means no errors were found and all sheet metal parts should have working flat patterns.

 

Let me know if you use Autodesk Vault, since there are some extra lines and extra work on your end to make sure the Vault Add-In doesn't cause problems.  After that question is answered, I can insert some other lines of code to speed up this code a fair amount.

 

SyntaxEditor Code Snippet

Dim oCompDef As SheetMetalComponentDefinition
Dim ErrorList As New ArrayList
ErrorCount = 0
For Each oDoc As Document In ThisApplication.ActiveDocument.AllReferencedDocuments
	If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		oCompDef = oDoc.ComponentDefinition
		If oCompDef.HasFlatPattern = True Then Continue For
		If oCompDef.HasFlatPattern = False Then ThisApplication.Documents.Open(oDoc.FullFileName, True)
		Try
			oCompDef.Unfold
			oCompDef.FlatPattern.ExitEdit
			oDoc.Save
		Catch
			ErrorList.Add(oDoc.DisplayName)
			ErrorCount += 1
		Finally
			oDoc.Close(True)
		End Try
	End If
Next
If ErrorCount > 0 Then
	oWrite = System.IO.File.CreateText(ThisDoc.PathAndFileName(False) + " Flat Pattern Errors.txt")
	oWrite.WriteLine(iProperties.Value("Project", "Part Number") + " Flat Pattern Errors")
	oWrite.WriteLine()
	For Each i In ErrorList
		oWrite.WriteLine(i)
	Next
	oWrite.Close
	Process.Start("notepad.exe", ThisDoc.PathAndFileName(False) + " Flat Pattern Errors.txt")
End If
MessageBox.Show("Flat Pattern Check Completed.", "Flat Pattern Extractor", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

Good Luck.

Message 8 of 17

Carl_mgfx
Advocate
Advocate

@philip1009

 

Thanks for the code, it works like a charm!  Using the multi-body sheet metal work flow gives me parts that are derived from the original multi-body, I see the code lists this original file as an error, but this is acceptable for me.

 

At the moment I'm not too worried about Vault integration but that would be a nice addition.  Once again, thank you for the code and I will spend some time learning from it!


Kind Regards
Carl van Rooyen
Application Engineer

Inventor 2019, Vault 2019,
HP ZBook 15u G4, Win 10 64Bit (ver 1709), i7-7500U, 16 GB Ram
Intel HD Graphics 620 with AMD Firepro W4190M
0 Likes
Message 9 of 17

philip1009
Advisor
Advisor

The only thing to worry about for the Vault Add-In is if you don't have a file checked out while perusing an assembly list, you'll get a prompt to check out from vault for every file.  Unfortunately the only way around this is to deactivate the Add-In at the beginning of the code, and then reactivate it at the end.  Just make sure that any sheet metal parts you're working on are checked out or they won't save properly.

 

SyntaxEditor Code Snippet

ThisApplication.ApplicationAddIns.ItemById("{48B682BC-42E6-4953-84C5-3D253B52E77B}").Deactivate

ThisApplication.ApplicationAddIns.ItemById("{48B682BC-42E6-4953-84C5-3D253B52E77B}").Activate
Message 10 of 17

AlexFielder
Advisor
Advisor

The issue you'll have with deactivation of the Vault add-in is that you won't then get prompted for those sheet metal files that need checking out, so you'd have to do that manually.

 

This (checking if a specific referenced item is checked out) is something a colleague asked me about and as-yet I haven't found an elegant solution. There was this post however today:

 

https://forums.autodesk.com/t5/inventor-customization/vba-question/td-p/8235969

 

which in turn linked to this post I had not seen before:

 

https://forums.autodesk.com/t5/inventor-customization/file-is-a-new-file-not-known-to-vault/td-p/785...

 

Is this a better solution? (I haven't tested it yet, so I have no idea)

0 Likes
Message 11 of 17

Carl_mgfx
Advocate
Advocate

@AlexFielder & @philip1009

 

Thanks for the replies, these will help if I ever need it.  For now you guys have been a huge help and I've got what I need working.  Thanks a ton! 


Kind Regards
Carl van Rooyen
Application Engineer

Inventor 2019, Vault 2019,
HP ZBook 15u G4, Win 10 64Bit (ver 1709), i7-7500U, 16 GB Ram
Intel HD Graphics 620 with AMD Firepro W4190M
0 Likes
Message 12 of 17

dusan.naus.trz
Advisor
Advisor

Hi,
this topic is very interesting. And your code is nice. The problem is that it does not work for components that are created as derived. I have a part and more bodies (multi body) in it, and I convert it to an assembly. I want to use a flattening and throw an error at the end.
Is it possible to treat this error? What do you think?

0 Likes
Message 13 of 17

philip1009
Advisor
Advisor

I'll look into it when I'm able to, I don't work with multi-body parts very often so it'll take a bit of research and perusing the API.  First I have to find out if it's even possible since a lot of the recent multi-body updates haven't been exposed in the API as of 2019.4, hence why I don't use them as much as I would like to.

0 Likes
Message 14 of 17

dusan.naus.trz
Advisor
Advisor

Hi,
thank you for your time and interest. When I use mirroring in a report, the component is derived. Or when I use multibody then the components are derived in the assembly. The rule could work as follows. If it has part more than 2 body Then Continue For. But I don't know how to write it right.

0 Likes
Message 15 of 17

Anonymous
Not applicable

IN CASE YOU HAVENT FIRGURED THIS OUT.... CHANGE  " AllReferencedDocuments" to "ReferencedDocuments" and it will not open the Base-Component

 


@dusan.naus.trz wrote:

Hi,
thank you for your time and interest. When I use mirroring in a report, the component is derived. Or when I use multibody then the components are derived in the assembly. The rule could work as follows. If it has part more than 2 body Then Continue For. But I don't know how to write it right.


 

0 Likes
Message 16 of 17

dusan.naus.trz
Advisor
Advisor

Hi,
this is a solution for sheet metal created by solidbody in Assembly. This was my question and here is the solution.

https://forums.autodesk.com/t5/inventor-customization/ilogic-part-contains-solidbody/m-p/9151668#M10...

0 Likes
Message 17 of 17

Anonymous
Not applicable

I realize what the question is, all to well lol.. that's my workflow as well... Base Component (Internal Volume; Water, Gas, Air) > Derived Sheet Metal Components for each Sheet metal style (Multi Body) > Parts/Assembly... from the assembly, I run a rule to open only the sheet metal parts and flatten, check for bends, flip the face to be "up", export dxf, etc... and the syntax 

'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments

AllReferencedDocuments, as you know, opens every document and all referenced documents...the derived of the derived of the derived... once I changed it it ReferencedDocuments it doesn't look past the part files.

 

I tried arguing for multibody but would still open the Base Component if it had one body and and Sheet Metal iPart. 

0 Likes