iLogic script for automation iAssembly drawings

iLogic script for automation iAssembly drawings

21104662
Observer Observer
373 Views
1 Reply
Message 1 of 2

iLogic script for automation iAssembly drawings

21104662
Observer
Observer

Dear engineers,

 

I have created several iAssemblies with annotations in them. I want to send all these assemblies to someone in the form of PDF and DXF files. During the process of creating the IDW file, which I intended to convert to PDF and DXF files, it occurred to me that it would be much easier to write a code in iLogic that will do it for me. 

 

The code you see below works, but it has several issues. One of the initial problems is that it begins generating files, But the first few files have the name of the place in the table where they are supposed to go, but the assembly does not change. So, I have a couple of PDF and DXF files with different names, but they contain the same assembly.

 

Another problem I have with the code is that it only works on small assemblies, assemblies with very few parts in them. The issue I tried to describe earlier pertains to small assemblies. With larger assemblies, it does not create a PDF or DXF file at all.

 

Dim oDrawingDoc As DrawingDocument
oDrawingDoc = ThisApplication.ActiveDocument
oPath = ThisDoc.Path

oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

Dim oView As DrawingView
oView = oDrawingDoc.ActiveSheet.DrawingViews(1)

Dim oAssemblyDoc As AssemblyDocument
oAssemblyDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument

Dim oDef As AssemblyComponentDefinition
oDef = oAssemblyDoc.ComponentDefinition

Dim sPath As String

'------------------ PDF DWG Stuff ------------------------------------

'Declareren
oRevNum = iProperties.Value("Project", "Revision Number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

'PDF plugin laden en configureren
oOptions.Value("All_Color_AS_Color") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets


'2D bestand naar PDF folder zoeken
oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"
'PDF folder aanmaken als deze niet bestaat
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If


'2D bestand naar Autocad dwg folder zoeken	
bFolder = Left(oPath, InStrRev(oPath, "\")) & "Autocad dwg"	
'Autocad dwg folder aanmaken als deze niet bestaat
If Not System.IO.Directory.Exists(bFolder) Then
	   System.IO.Directory.CreateDirectory(bFolder)
End If




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

If oDef.IsiAssemblyMember = True Then
    Dim iAssemblyF As iAssemblyFactory
    iAssemblyF = oDef.iAssemblyMember.ParentFactory

		

    For i = 1 To (iAssemblyF.TableRows.Count-1)
	'For Each oTab As iAssemblyTableRow In iAssemblyF.TableRows
        oAssemblyDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
        
        'iAssemblyF.ChangeRow("", oTab.MemberName)
        sPath = Replace(oAssemblyDoc.FullFileName, iAssemblyF.FileNameColumn(i).Value, iAssemblyF.FileNameColumn(i + 1).Value)
        
        Call iAssemblyF.CreateMember(i + 1)
        
        Call oView.ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(sPath)
        
        Call oDrawingDoc.Update2(True)
		
		'Timer
        PauseTime = 5 'seconds
        Start = Timer
        Do While Timer < Start + PauseTime
        ThisApplication.UserInterfaceManager.DoEvents		
		Loop

		
		
		Exportname = "\" & iAssemblyF.FileNameColumn(i + 1).Value

        'Export seperate idw for each iAssembly
        'Call oDrawingDoc.SaveAs(oPath & "\" & iAssemblyF.FileNameColumn(i + 1).Value & ".idw", True)

		'PDF opslaan met membername
		pdf_exportname = oFolder & "\" & Exportname & ".pdf"
		oDataMedium.FileName = pdf_exportname
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)	
		
		'Autocad dwg opslaan met membername
		dwg_exportname = bFolder & "\" & Exportname & ".dwg"
		ThisDoc.Document.SaveAs(dwg_exportname , True)	
		
		
		

    Next
End If

 

I hope someone would like to help me so I can make further progress with my project.

Thanks for the effort.

Tim Duivenvoorde

Reply (1)
Message 2 of 2

A.Acheson
Mentor
Mentor

Hi @21104662 

 

So in testing everything worked fine as long as you remember to place the initial drawing view to member one. If you fail to do so then no drawing view reference is replaced. The below code has a loop to determine the active member and then replace the reference to make member 1 active therefore starting the loop in the right location at index 1.

I would caution pdf creation straight after drawing changes as the annotation tend to move around a lot when geometry size changes so you can get annotations overlapping and  in poor locations. It would be best to do a final check before pdf creation. 

Dim oDrawingDoc As DrawingDocument
oDrawingDoc = ThisApplication.ActiveDocument
oPath = ThisDoc.Path

oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

Dim oView As DrawingView
oView = oDrawingDoc.ActiveSheet.DrawingViews(1)

Dim oAssemblyDoc As AssemblyDocument
oAssemblyDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument

Dim oDef As AssemblyComponentDefinition
oDef = oAssemblyDoc.ComponentDefinition

Dim sPath As String

'------------------ PDF DWG Stuff ------------------------------------

'Declareren
oRevNum = iProperties.Value("Project", "Revision Number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

'PDF plugin laden en configureren
oOptions.Value("All_Color_AS_Color") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets


'2D bestand naar PDF folder zoeken
oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"
'PDF folder aanmaken als deze niet bestaat
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If


'2D bestand naar Autocad dwg folder zoeken	
bFolder = Left(oPath, InStrRev(oPath, "\")) & "Autocad dwg"	
'Autocad dwg folder aanmaken als deze niet bestaat
If Not System.IO.Directory.Exists(bFolder) Then
	   System.IO.Directory.CreateDirectory(bFolder)
End If

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

If oDef.IsiAssemblyMember = True Then
   
   Dim iAssemblyF As iAssemblyFactory
    iAssemblyF = oDef.iAssemblyMember.ParentFactory
	
	'[Ensure iAssembly member Is always Member 1 when starting loop
	For i = 1 To (iAssemblyF.TableRows.Count)
		memberName1 = iAssemblyF.FileNameColumn.Item(1).Value
		If oAssemblyDoc.FullFileName.Contains(iAssemblyF.FileNameColumn(i).Value) Then
			currentMemberName = iAssemblyF.FileNameColumn(i).Value
		End If
 	Next
	
	sPath = Replace(oAssemblyDoc.FullFileName, currentMemberName, memberName1)
 	
	Call oView.ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(sPath)
    Call oDrawingDoc.Update2(True)
	']

    For i = 1 To (iAssemblyF.TableRows.Count-1)

        oAssemblyDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
        
        sPath = Replace(oAssemblyDoc.FullFileName, iAssemblyF.FileNameColumn(i).Value, iAssemblyF.FileNameColumn(i + 1).Value)
       
	    'Error checking path change
	    If sPath = oAssemblyDoc.FullFileName Then
		   Logger.Info("Error setting Path")
	    Else
			Logger.Info("Path changed Success")
	    End If

        Call iAssemblyF.CreateMember(i + 1)
        
        Call oView.ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(sPath)
        
        Call oDrawingDoc.Update2(True)
		
		'Timer
        PauseTime = 5 'seconds
        Start = Timer
        Do While Timer < Start + PauseTime
        ThisApplication.UserInterfaceManager.DoEvents		
		Loop

		Exportname = "\" & iAssemblyF.FileNameColumn(i + 1).Value

		'PDF opslaan met membername
		pdf_exportname = oFolder & "\" & Exportname & ".pdf"
		oDataMedium.FileName = pdf_exportname
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)	
		
		'Autocad dwg opslaan met membername
		dwg_exportname = bFolder & "\" & Exportname & ".dwg"
		
		ThisDoc.Document.SaveAs(dwg_exportname, True)	
		
    Next
End If

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan