Dxf generator for all open assemblies.

Dxf generator for all open assemblies.

Anonymous
Not applicable
663 Views
5 Replies
Message 1 of 6

Dxf generator for all open assemblies.

Anonymous
Not applicable

Hello, I have an external rule exporting flat patterns from assemblies (similar can be found on this forum, mine's a bit modified) and I would like to run it for all open assemblies in inventor (2018, professional). Basicly i want to put this dxf generator in some kind of loop, open all *.iams I need to and run it. I tried with some counters active docs, with "for each" loops, "do while" loops. the best results i get from simple for each loop. it ran generator for every open iam (generator has prompting windows and all behave well, they are prompting), which is cool, BUT generates dxfs only for first, active iam, from which I started. Is it possible to put this generator in loop for all open iams? Can someone smarter help me? 

 

Thank You in advance, and sorry for lousy english. also some parts of code are in polish. Also for working generator You have to have a folder named #DXF in folder where main iam is.

 

Mateusz.

 

for each:

 

Dim aDoc As AssemblyDocument
aDoc = ThisApplication.ActiveDocument
Dim iDoc As Document

For Each iDoc In aDoc.AllReferencedDocuments
      	  iLogicVb.RunExternalRule("generator dxfow.txt")
	  ThisDoc.Document.Close(True)
Next

 

dxf generator:

Class ThisRule
Dim partDox As Document
Sub Main()
Dxf()
Zamk()
End Sub
Sub Dxf()
    If MsgBox("Ta regula wygeneruje pliki dxf dla wszystkich plikow. Czy chcesz kontynuowac?", vbOKCancel + vbExclamation, "Batch Write Flat Pattern") = vbCancel Then Exit Sub
    partDox = ThisDoc.Document
    ' Sets up the variable "MyFile" to be each file in the directory
    ' This example looks for all the files that have an .ipt extension.
    ' This can be changed to whatever extension is needed. Also, this
    ' macro searches the current directory. This can be changed to any
    ' directory.
    Dim Directory As String
    Directory = ThisDoc.Path
    
    'search for files.
    Dim docFile As Document
    
    ' Starts the Loop, which will Continue Until there are no more files
    ' found.
    For Each docFile In partDox.AllReferencedDocuments
        ' Displays a message box with the name of the file. This can be
        ' changed to any procedure that would be needed to run on every
        ' file in the directory such as opening each file.
        'opens file:
        
        partDox = ThisApplication.Documents.Open(docFile.FullFileName, False)
        ThisApplication.SilentOperation = True
        'Test to make sure we opened a sheet metal part
        If(docFile.DocumentSubType.DocumentSubTypeID.Equals("{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}")) Then
            DefaultChoice = True
			   'MsgBox(MyFile)
'----------------------------------------------------------------------------------				
'----------------------------------------------------------------------------------				
			Dim oPropSet As PropertySet
			Dim oProp As Inventor.Property
			Dim invPartNoProperty As Inventor.Property = partDox. _
			PropertySets.Item("Design Tracking Properties").Item("Part Number")				
			PartNumber = invPartNoProperty.Value
			Dim invDescriptionProperty As Inventor.Property = partDox. _
			PropertySets.Item("Design Tracking Properties").Item("Description")
			Description = invDescriptionProperty.Value
			Dim invMaterialProperty As Inventor.Property = partDox. _			
			PropertySets.Item("Design Tracking Properties").Item("Material")
			Mat = invMaterialProperty.Value
'			Dim invGrubProperty As Inventor.Property = partDox. _			
'			PropertySets.Item("Design Tracking Properties").Item("Thickness")
			namex = docFile.FullFileName
			name = IO.Path.GetFileName(namex)
			
			Grub = Parameter(name, "Grubość")
			'MsgBox(Grub)
			
'-----------------------------------------

			
'----------------------------------------------------------------------------------		
							Dim FilePATH As String = "FilePATH"
						
						customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")
						
						Try
						
								prop= customPropertySet.Item(FilePATH)
						
						Catch
						
									customPropertySet.Add("", FilePATH)
						
						End Try
						
						If iProperties.Value("Custom", "FilePATH") = "" Then
						
						iProperties.Value("Custom", "FilePATH") = "C:\"
						
						Else
						
						End If
						
						'Dim partDoc As PartDocument
						
'						If partDox.Document.DocumentType <> kPartDocumentObject Then
'						
'						MessageBox.Show ("Please open a part document", "iLogic")
'						
'						End If
						
						FilePATH = ThisDoc.Path & "\#DXF" 'InputBox("Enter a FilePATH for part file", "iLogic", iProperties.Value("Custom", "FilePATH"))
						
						iProperties.Value("Custom", "FilePATH") = FilePATH


'----------------------------------------------------------------------------------	

    'Dim oDoc As PartDocument

    'oDoc = partDox

    Dim oCompDef As SheetMetalComponentDefinition

    oCompDef = partDox.ComponentDefinition

'     If oCompDef.HasFlatPattern = False Then
'
'     oCompDef.Unfold
'
'    Else
'
'    oCompDef.FlatPattern.Edit

'    End If

    Dim sOut As String

    Dim sPATH As String

    sPATH = iProperties.Value("Custom", "FilePATH")

    sOut = "FLAT PATTERN DWG?AcadVersion=2004&OuterProfileLayer=Burn&InteriorProfilesLayer=Burn&InvisibleLayers=IV_UNCONSUMEND_SKETCHES;IV_ALTREP_BACK;IV_ALTREP_FRONT;IV_ARC_CENTERS;IV_TOOL_CENTER_DOWN;IV_TOOL_CENTER;IV_ARC_CENTERS;IV_TANGENT;IV_BEND;IV_BEND_DOWN&SplineTolerance Double 0.01"'"FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_INTERIOR_PROFILES"

    Dim sFname As String
    'Name = InputBox("Please enter your file name", "Save As", "")

	sFname = sPATH & "\" & PartNumber & " " &" - " & Mat & " " & Grub &  " mm - szt " &  ".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

'This code has been adapted from http://www.cadlinecommunity.co.uk/Blogs/Blog.aspx?ScoId=

'4733ef2d-cd48-4bd9-a280-1d88dbbf3556&returnTo=%2fBlogs%2fclintonbrown%2fDefault.aspx

'&returnTitle=Clinton+Brown%20Blog
'----------------------------------------------------------------------------------	
        End If
        
        partDox.Close(False)
        partDox = Nothing
        ThisApplication.SilentOperation = False
    Next
    'MsgBox("Grubosc: " & Grub, vbOKOnly)
    MsgBox("Wszystkie pliki zostały poprawnie zapisane do folderu #DXF.", vbOKOnly, "Batch Write Flat Pattern")

End Sub 

Sub Zamk()
ThisDoc.Document.Close(True)
End Sub

End Class


 

 

 

 

0 Likes
664 Views
5 Replies
Replies (5)
Message 2 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Hoping that below iLogic code would helps get all assembly documents which are opened in Inventor.

 

Sub Main()
	Dim oDoc As Document 
	For Each oDoc In ThisApplication.Documents 
		If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Dim oAssyDoc As AssemblyDocument 
			oAssyDoc = oDoc
			Dim iDoc As Document 
			For Each iDoc In oAssyDoc.AllReferencedDocuments
				Messagebox.Show(iDoc.Displayname)
			Next
		End If 
	Next
End Sub

Please feel free to contact if there is any queries.

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 6

dgreatice
Collaborator
Collaborator

hi @chandra.shekar.g,

 

I think you had little missed.

 

 

Sub Main()
	Dim oDoc As Document 
	For Each oDoc In ThisApplication.Documents.VisibleDocuments
		If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Dim oAssyDoc As AssemblyDocument 
			oAssyDoc = oDoc
			oAssyDoc.Activate
'Continue you code here End If Next End Sub
Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
Message 4 of 6

Anonymous
Not applicable

@dgreatice @chandra.shekar.g Thank You both for Your help. 

 

Firstly, I'd like You to know that I have no programming background. All I learnt of iLogic is from trial and error method.

So. When I paste my part of generator into Yours code i get multiple errors:error3.jpg 

error in row2: class must ends with end class

error in row3: Element Class „LmiRuleScript” must implements element „Sub RuleParametersInit()” for interface „Autodesk.iLogic.Interfaces.IRuleInterface”.

error in row3: Element Class „LmiRuleScript” must implements element „Sub RuleParametersOutput()” for interface „Autodesk.iLogic.Interfaces.IRuleInterface”.

error in row14: for must ends with next

error in row15: if must ends with end if

error in row31 and 34: interface „Autodesk.iLogic.Interfaces.IRuleInterface” is not implemented by this class.

error in row35,18,25...159,167: A reference to a non-shared member requires a reference to the object

error in row174: end if must be preceded by if

error in row175: Syntax error.

error in row174: end sub must be preceded by sub

 

I tried couple of times changing Your idea: running external rule, changing Yours Sub Main() into Window() sub and adding it in original code of generator: 

 

Sub Main()
Window()
Zamk()
Dxf() End Sub Sub Window() Dim oDoc As Document For Each oDoc In ThisApplication.Documents.VisibleDocuments If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim oAssyDoc As AssemblyDocument oAssyDoc = oDoc oAssyDoc.Activate End If Next End Sub Class ThisRule Dim partDox As Document Sub Dxf()

When I put "run external rule" into Your code, it runs this external rule but again, only for one iam and then i get this:

error.jpg

 

undefined error (exception from hresult ...)

 

Also, I think Your code alone doesnt do what i need it to. it returns names of parts and assemblies (ipt and iam as well). those that ends with -00 are assemblies, those which ends with number -01; -05 ; and on and on and on are parts. But as i wrote before i dont know much of programming and maybe this is good.parts.jpg

So.  I think Your code works, but not in the way i need.

 

Kind regards 

 

Mateusz

0 Likes
Message 5 of 6

dgreatice
Collaborator
Collaborator

1. Remove class

2. Don't Separate process when loop each Document 

 

Sub Main()

Dim oDoc As Document

For Each oDoc In ThisApplication.Documents.VisibleDocuments

If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

Dim oAssyDoc As AssemblyDocument

oAssyDoc = oDoc

oAssyDoc.Activate

Dxf()

Zamk()

End If

Next

End Sub

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 6 of 6

Anonymous
Not applicable

Thank You, but i still get this error w exception. Any solution?error2.jpg

 

 

I also turned off this this Zamk() sub which closes active iam. i didnt get this exception error, however it does generate dxfs only for first, active iam

0 Likes