Compile a new Drawing from multiple Drawings
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm working on this piece of code and I would like to compile .dwg drawing files:
- I have existing .dwg drawings of Assemblies and Sub-Assemblies (or Parts)
- An Excel Spreadsheet contains some info, including a column with the text "Assembly" for any top level Assembly. I would like to compile a new Drawing made up of any "Sub Drawings" which would be involved in an Assembly.
My method was this, but I'm more than happy to receive input, to better it:
Look thru Excel spreasheet
If "Assembly" is found
Copy first 3 Letters from column "A"
If a .dwg Drawing in the folder contains these numbers
Open this Drawing (It may be better to open drawings silently? Copy and Close, etc.?)
Iterate thru Parts list found on first page of Drawing and open any other drawings of those parts
It should iterate until there isn't anything new to open (not sure if the way I have it does this well)
Open a New Drawing from Template
Copy all opened drawings to New Drawing
Sort Sheets Alphabetically
Save New Drawing
Close All .dwg files - I suppose I would need an .ipt opened to be able run the code from
Do this for all files containing "Assembly" in Excel
I've been working on this for a while and for now bogged down at Opening New Drawing and copying all sheets to it.
Also the iteration to open files may not quite be working perfectly. It seems to try to open the file multiple times.
I'm very grateful for any help!
Public Sub Main() 'Open Excel Spreadsheet. Read column "C" -> Search for "Assembly" GoExcel.Open(ExcelFile, "Sheet1") For i = 3 To 300 If GoExcel.CellValue("C" & i) = ("Assembly") Dim ProjNum As String = GoExcel.CellValue("A" & i) Dim FileLocation As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(WorkFolder) Dim fi As System.IO.FileInfo() = FileLocation.GetFiles("*" & ProjNum & "*") For Each oFile As System.IO.FileInfo In fi 'Get only DWG files - Not PDFs or other files DwgFile = Left(oFile.FullName, Len(oFile.FullName) -3) & "dwg" Try open = ThisApplication.Documents.Open(DwgFile, True) Catch 'MessageBox.Show("Drawing Is Already Opened", "Title") End Try Next 'Call Sub to Open Drawings of Sub-Assemblies and Sub-Parts found on first sheet of Drawing OpenPartDwg 'Open a New Drawing Document ".dwg" from Template Dim oDoc As Document 'Location of Drawing Template oCopyFiler = "D:\Drawings Test\cf.dwg" 'Open Template open = ThisApplication.Documents.Open(oCopyFiler, True) 'Save New Drawing as "ProjectNumber, AssemblyName, WeldmentName + "Complete Drawings" " Dim AsName As String = GoExcel.CellValue("B" & i) Dim WeldmentName As String = GoExcel.CellValue("D" & i) Dim DrawingName As String DrawingName = ProjNum & " - " & AsName & " - " & WeldmentName & " - " & "Complete Drawings" 'MessageBox.Show(DrawingName) ' New Location to Save Compiled Drawing Dim oNewSubPath As String = "\Complete Assembly Drawings" oNewPath = WorkFolder & oNewSubPath 'New Drawing Save As oDoc.SaveAs(oNewPath & DrawingName & ".dwg", False) 'overwrite file if one already exists Dim oNewDrawing As DrawingDocument = ThisApplication.Documents.Open(DrawingName,False) For Each oDoc In ThisApplication.Documents.VisibleDocuments If oDoc.DocumentType = kDrawingDocumentObject Then 'Copy all sheets of all opened kDrawingDocs into the New Opened Template For Each oSheet As Sheet In oDoc.Sheets oSheet.CopyTo(oNewDrawing) Next End If Next oDoc 'Call Sub to rename sheets RenameSheets 'Call Sub to sort all Drawing Sheets alphabetically SortDrawingSheets 'Re-Save the New Created Document ThisDoc.Save 'Close all opened Drawing files Dim oApp As Inventor.Application = ThisApplication For Each oDoc In oApp.Documents.kDrawingDocumentObject oDoc.Close(True) Next End If Next End Sub Public Function ExcelFile As String Dim eFileName As String = "\TPB Project Numbers.xlsx" Dim eFile As String = WorkFolder & eFileName Return eFile End Function Public Function WorkFolder As String Dim wFolder As String = "D:\Drawings Test\1 Drawings" Return wFolder End Function Public Sub OpenPartDwg Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument Dim oDrawingView As DrawingView Dim oSheet As Sheet = oDrawDoc.ActiveSheet Dim oPartList As PartsList oPartList = oSheet.PartsLists(1) Dim drawBomRow As DrawingBOMRow Dim refDoc As Document For Each oPartListRow In oPartList.PartsListRows If oPartListRow.Visible = "True" Then drawBomRow = oPartListRow.ReferencedRows.Item(1) refDoc = drawBomRow.BOMRow.ComponentDefinitions.Item(1).Document FilePath = refDoc.FullFileName() FilePath = Left(FilePath, Len(FilePath) -3) & "dwg" For j = 1 To oPartList.PartsListRows.Count oCell = oPartList.PartsListRows.Item(j).Item("PART NUMBER") Dim oItemValue As String oItemValue = oCell.Value PartProj = Left(oItemValue,3) 'MessageBox.Show(PartProj, "This is the Part Project Number:") Dim FileLocation As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(WorkFolder) Dim fi As System.IO.FileInfo() = FileLocation.GetFiles("*" & PartProj & "*") For Each oFile2 As System.IO.FileInfo In fi 'MessageBox.Show(oFile2, "Drawing Name:") DwgFile = Left(oFile2.FullName, Len(oFile2.FullName) -3) & "dwg" Try open = ThisApplication.Documents.Open(DwgFile, True) Catch 'MessageBox.Show("Drawing Is Already Opened", "Title") End Try Next Next End If Next End Sub 'Sort Drawing Sheets by Sheet Name (equals Part Name) Public Sub SortDrawingSheets Dim drawingDoc As DrawingDocument = ThisDoc.Document Dim sheet As Sheet = Nothing Dim sheetsList As New List(Of sheet) Dim browserPane As BrowserPane = drawingDoc.BrowserPanes.Item("Model") For Each sheet In drawingDoc.Sheets sheetsList.Add(sheet) Next sheetsList.Sort(AddressOf Comparer) For Each sheet In sheetsList Dim sheetNode As BrowserNode = browserPane.GetBrowserNodeFromObject(sheet) Dim bottomNode As BrowserNode = browserPane.TopNode.BrowserNodes.Item(browserPane.TopNode.BrowserNodes.Count) browserPane.Reorder(bottomNode, False, sheetNode) Next End Sub Private Function Comparer(x As Sheet, y As Sheet) As Integer Return String.Compare(x.Name, y.Name) End Function Public Sub RenameSheets If TypeOf ThisDoc.Document Is DrawingDocument Then Dim dwgDoc As DrawingDocument = ThisDoc.Document For Each dwgSheet As Sheet In dwgDoc.Sheets If dwgSheet.DrawingViews.Count > 0 Then modelFile = dwgSheet.DrawingViews(1).ReferencedDocumentDescriptor.FullDocumentName modelDoc = dwgSheet.DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument prtNumber = modelDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value If Not String.IsNullOrEmpty(prtNumber) Then dwgSheet.Name = prtNumber End If End If Next End If End Sub