Export Assembly Parts to Individual Drawings

Export Assembly Parts to Individual Drawings

zacharyEZG9N
Community Visitor Community Visitor
407 Views
1 Reply
Message 1 of 2

Export Assembly Parts to Individual Drawings

zacharyEZG9N
Community Visitor
Community Visitor

Hi All,

 

I've scoured these forums for an iLogic code to suit my needs and what I've come across seems to be getting close to what I am chasing. 

 

The following code exports all files in my assembly to my own drawing template - Great! The problem being is that all the parts are inside the one drawing and on the same sheet. In an ideal world they'd be all exported to their own drawings with the suffix _drawing (or something easily distinguishable from their part file name), saved in the part file location, with the flat pattern view, if it exists (if not the drawing wont need to be created) and the folded model. I don't care about the scale or views I'm looking at yet, I'll update that as I go through each drawing. It's just a huge time suck creating each of these. 


Is anyone able to support here?  I'd also be able to compensate anyone for their time in setting up working code. (Setting up each view, scale and adding bend details to the sheet metal parts could be additional compensated work $) 

 

PS: I know nothing, and I mean diddly squat, about coding... 

 

The code is as follows: 

 

Sub Main
    Dim asmDoc As Document = ThisApplication.ActiveDocument
    
    If asmDoc Is Nothing Then Exit Sub
    
    If asmDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
        MsgBox("This iLogic rule is intended to run only for assembly documents.", vbExclamation)
        Exit Sub
    End If
    
    Dim asmDocument As AssemblyDocument = TryCast(asmDoc, AssemblyDocument)
    If asmDocument Is Nothing Then
        MsgBox("Failed to cast the active document to AssemblyDocument.", vbExclamation)
        Exit Sub
    End If
    
    Dim drawingDoc As DrawingDocument = CreateDrawingDocument()
    If drawingDoc Is Nothing Then Exit Sub
    
    For Each occurrence As ComponentOccurrence In asmDocument.ComponentDefinition.Occurrences
        Dim partDoc As Document = occurrence.Definition.Document
        If partDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
            AddPartViewToDrawing(partDoc, drawingDoc)
        End If
    Next
    
    Dim drawingFileName As String = System.IO.Path.ChangeExtension(asmDocument.FullFileName, ".idw")
    drawingDoc.SaveAs(drawingFileName, False)
    
    MsgBox("Drawing created successfully: " & drawingFileName, vbInformation)
End Sub

Function CreateDrawingDocument() As DrawingDocument
    Dim templatePath As String = "C:\Users\Public\Documents\Autodesk\Inventor 2023\Templates"
    Dim templateName As String = "DWG Template.idw"
    Dim templateFullFileName As String = System.IO.Path.Combine(templatePath, templateName)
    
    If Not System.IO.File.Exists(templateFullFileName) Then
        MsgBox("Template Drawing '" & templateName & "' could not be found in '" & templatePath & "'.", vbCritical)
        Return Nothing
    End If
    
    Dim drawingDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, templateFullFileName, True)
    If drawingDoc Is Nothing Then
        MsgBox("Failed to create the drawing document.", vbCritical)
        Return Nothing
    End If
    
    Return drawingDoc
End Function

Sub AddPartViewToDrawing(partDoc As Document, drawingDoc As DrawingDocument)
    Dim baseView As DrawingView = drawingDoc.ActiveSheet.DrawingViews.AddBaseView(partDoc, ThisApplication.TransientGeometry.CreatePoint2d(5, 5), 1.0, ViewOrientationTypeEnum.kFrontViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)
    
    ' Additional settings for the view if needed
    baseView.Position = ThisApplication.TransientGeometry.CreatePoint2d(5, 5)
    ' Set other properties of the view as needed
End Sub

 

0 Likes
408 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor

Hi @zacharyEZG9N.  It sounds like you are asking for a complete automation code solution for a relatively complex process, but would not be able to understand, customize, or maintain any code example that others may be able to offer.  However, since it appears you did take the time to assemble some code to start with for this task, making some changes here or there will not be as complex or time consuming as it could have been without it.  Running a code like this on a large assembly may slow or freeze Inventor while it runs, and no guarantees about how long it will take to run either.  Since we are able to open or create Inventor documents by code, without them necessarily needing to be visible, I changed one line to try to make all those new drawings without them being visible, in an attempt to speed the process up a bit.  Below is another example iLogic rule code you can try out, which does a very similar task, but does it slightly differently.  This code should create a new drawing for every referenced part in the assembly that it can not find an existing drawing for.  It will attempt to place one base view in the center of the default sheet of each drawing, then save that drawing to a file, then close it.  As it is doing these things, it is recording the full path & file name of each new drawing it is creating, and writing these to the iLogic Log window.  However, you must make sure that window is showing before starting the rule, otherwise it will not capture this information.  It is another tab available beside the model tab of your model browser tree.  If it is not showing, make sure it is showing first, before running this rule, just for the added information.  This rule also uses a ProgressBar, which will show on your screen while it is running.  It will show the total number of referenced documents, and the progress bar will advance for each one it checks / processes along the way.  If the process is taking way too long, you can click the Cancel button on that ProgressBar to stop the progress.  This will not undo what has been done so far, just stop further processing of referenced documents.  Sort of like a life line, if it gets bogged down.  There should also be a message show when the process is done, letting you know it has finished.

The code is within the attached text file.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes