Showing posts with label vb. Show all posts
Showing posts with label vb. Show all posts

Friday, 8 January 2016

Batch Assembly Export

Not enough time today for a proper post just a quick one. I have been working on a vb-ilogic code to identify and resolve all references. On the same process I hope to get a code for renaming the files as well.

This will of course be free and available for everybody to download from this blog but you need to stick around and check my progress because I don’t have a time frame of when that will be available.

Replacing references is working but we get into errors and limitations for special files like skeletons in Frame Generator, Tube and Pipe Route and Runs, welded assemblies, documents marked as Reference in BOM, etc. It’s a slow progress and I can’t allocate it as much time as I would like at the moment.

On a side note, there is a NDA (Non Disclosure Agreement) between me and Autodesk so I can’t say much but I was assured that the T&P list is very important for them.

Enough said on that and for today I will share a bit of code that hopefully will get your productivity up and get those manual tasks to minimum. The code needs to be run from inside an assembly and will save all parts in STEP format in a folder right next to the main assembly. I have done this code for a friend and while he is still testing you can have a preview of it (hurry up Karol).

The SaveAs option in the code can be used with any formats listed in the Save Copy As dialog but you might want to investigate the results and use the ApplicationAddIns and HasSaveCopyAsOption which will give you more control over what gets exported and some of the options in the save as dialog.

The Save As dialog can be bypassed and process the assembly as a batch

If, for example, you need to export the current drawing to PDF you can’t (shouldn’t) use the SaveAs code because it will only export the first sheet and you can’t control, colors, resolution, line weights, printing sheet range, etc. There’s more info in my previous post here.

Here’s the code, enjoy:


 'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
    Exit Sub
End If
'get user input
RUsure = MessageBox.Show ( _
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If
'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
oPath = ThisDoc.Path
'get STEP target folder path
oFolder = oPath & "\" & oAsmName & " STEP Files"
'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".stp") , True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
'work the referenced models
For Each oRefDoc In oRefDocs
    Dim oCurFile As Document
    oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    oCurFileName = oCurFile.FullFileName
   
    'defines backslash As the subdirectory separator
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
   
    'find the postion of the last backslash in the path
    FNamePos = InStrRev(oCurFileName, "\", -1)  
    'get the file name with the file extension
    Name = Right(oCurFileName, Len(oCurFileName) - FNamePos)
    'get the file name (without extension)
    ShortName = Left(Name, Len(Name) - 4)

    Try
        oCurFile.SaveAs(oFolder & "\" & ShortName & (".stp") , True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)



I have tested it and save the assembly in all these other formats, including Inventor iam:

Results of exporting the assembly.
 
'- - - - - - - - - - - - -Assembly - - - - - - - - - - - - 
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".iam") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".dwg") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".bmp") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".CATProduct") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".dwf") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".dwfx") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".gif") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".igs") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".ige") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".iges") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".jpg") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".jt") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".pdf") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".png") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".x_b") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".x_t") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".g") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".neu") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".sat") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".stp") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".ste") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".step") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".stl") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".tiff") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".xgl") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".zgl") , True)


Later,
ADS


Edit 16-04-14

this is the code to append revision number at the end of the files:




Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
    Exit Sub
End If
'get user input
RUsure = MessageBox.Show ( _
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If
'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
oPath = ThisDoc.Path
'get STEP target folder path
oFolder = oPath & "\" & oAsmName & " STEP Files"

'get the document revision to use in the new filename
oRevNumAsm = iProperties.Value("Project", "Revision Number")

'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName & "_Rev" & oRevNumAsm &(".stp") , True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
'work the referenced models
For Each oRefDoc In oRefDocs
    Dim oCurFile As Document
    oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    oCurFileName = oCurFile.FullFileName

   
    'defines backslash As the subdirectory separator
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
   
    'find the postion of the last backslash in the path
    FNamePos = InStrRev(oCurFileName, "\", -1)  
    'get the file name with the file extension
    Name = Right(oCurFileName, Len(oCurFileName) - FNamePos)
    'get the file name (without extension)
    ShortName = Left(Name, Len(Name) - 4)
    oRevDoc = iProperties.Value(Name, "Project", "Revision Number")


    Try
        oCurFile.SaveAs(oFolder & "\" & ShortName & "_Rev" & oRevDoc & (".stp") , True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)



Thursday, 7 May 2015

Custom Table on Drawing

No time for a regular post or a video but I’ve got a small ilogic code to share on how to create and edit custom tables on drawings.
I am doing half a dozen tanks a week and each need to have a couple of tables with nozzle connection and tank specifications.
The tank is done as ilogic component that I clone along with the drawing each time a new one is needed.
The nozzle connection table looks like this:


And the Operating Conditions table:



                All the info comes from the model and if they are not present then we prompt the user for the values to fill in.
                It is better if you place and position the table before running the code otherwise it will show up somewhere in the lower left corner, (you can always move it of course). If you do place the table first even if it doesn't have the correct info the code will find it and will edit it.
                I am using table name as my search criteria when going through all the custom tables on the drawing.  The limitation would be having two custom tables with same name because it stops at the first one so, although obvious, make sure you have different names for each of your table; can’t call them all “table”.

        TableCheck:
'------------------------------------------------------
'---------------start finding the table----------------
'------------------------------------------------------
For i = 1 To oSheet.CustomTables.Count
    TableTitle = oSheet.CustomTables.Item(i).Title                       
    If TableTitle = "Operating Conditions" Then
        'Remember the table number for editing section
        TableNr = i
        'If table found jump to edit section
        Goto EditTable
    End If
Next
'------------------------------------------------------
'---------------end finding the table----------------
'------------------------------------------------------
               
If the table is not found then we are creating it and then we run the TableCheck again in order to find the table number to edit.

'CreateTable:
'------------------------------------------------------
'---------------start creating the table---------------
'------------------------------------------------------
' Set the column titles
oTitles = New String(){"Item", "Description"}
'
'
'
' Create the custom table
oCustomTable = oSheet.CustomTables.Add("Operating Conditions", ThisApplication.TransientGeometry.CreatePoint2d(15, 15), _
                                    2, 11, oTitles,oContents, oColumnWidths)
'------------------------------------------------------
'---------------finish creating the table---------------
'------------------------------------------------------
'edit the table by restarting the check routine
Goto TableCheck
       
This is where we edit the table, and we check if the model has the info we need and if not then we prompt the user for that. When we prompt we show the user the current value but allow him to change it if he wants:

EditTable:
'------------------------------------------------------
'---------------start editing the table---------------
'------------------------------------------------------
'Get the name Of the first model in the drawing
modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
'MessageBox.Show(modelName, "ilogic")
'
'
'
'
'------------------------------------------------------
'---------------finish editing the table---------------
'------------------------------------------------------

It’s pretty much the same for the Tank Connection Details so I won’t post that but the code for this one is down bellow for all those search engines and linked here for you to download.

Later,

ADS.



' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the active sheet.
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet

'Declare my custom table
Dim oCustomTable As CustomTable


TableCheck:
'------------------------------------------------------
'---------------start finding the table----------------
'------------------------------------------------------
For i = 1 To oSheet.CustomTables.Count
    TableTitle = oSheet.CustomTables.Item(i).Title                       
    If TableTitle = "Operating Conditions" Then
        TableNr = i
        'If table found jumpt to edit section
        Goto EditTable
    End If
Next

'if not found go create it
Goto CreateTable
'------------------------------------------------------
'---------------end finding the table----------------
'------------------------------------------------------


CreateTable:
'------------------------------------------------------
'---------------start creating the table---------------
'------------------------------------------------------
' Set the column titles
oTitles = New String(){"Item", "Description"}
' You can ask user for the name of the columns if you want
'Dim oTitles(1 To 2) As String
'oTitles(1) = "Item"
'oTitles(2) = InputBox("What to prompt", "iLogic", "Default value")

' Set the contents of the custom table (contents are set row-wise)
'more than one way to skin a cat
'Dim oContents(1 To 22) As String
oContents = New String(0 To 21){}

oContents(0) = "Contents"
oContents(1) = "" 'oMedia
oContents(2) = "Working Capacity (EST)"
oContents(3) = "" 'oCapEST
oContents(4) = "Dry Weight"
oContents(5) = "" 'oDryWgt
oContents(6) = "Maximum Working Weight"
oContents(7) = "" 'oMaxWgt
oContents(8) = "Specific Gravity"
oContents(9) = "" 'oSpecGrav
oContents(10) = "Max. Working Temperature"
oContents(11) = "5 Degrees C"
oContents(12) = "Max.Working Pressure"
oContents(13) = "Atmospheric"
oContents(14) = "Materials of Construction"
oContents(15) = "" 'oMaterial
oContents(16) = "Location"
oContents(17) = "Plantroom"
oContents(18) = "Tolerances"
oContents(19) = ""
oContents(20) = "Angular"
oContents(21) = "+/- 0.5 Degrees"


' Set the column widths (defaults to the column title width if not specified)
Dim oColumnWidths(0 To 1) As Double
oColumnWidths(0) = 7.5
oColumnWidths(1) = 6
 
' Create the custom table
oCustomTable = oSheet.CustomTables.Add("Operating Conditions ADS", ThisApplication.TransientGeometry.CreatePoint2d(15, 15), _
                                    2, 11, oTitles,oContents, oColumnWidths)
'------------------------------------------------------
'---------------finish creating the table---------------
'------------------------------------------------------
'edit the table by restarting the check routine
Goto TableCheck



EditTable:
'------------------------------------------------------
'---------------start editing the table---------------
'------------------------------------------------------
'Get the name Of the first model in the drawing
modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
'MessageBox.Show(modelName, "ilogic")
   
'declare the table to edit by the number found in check
oCustomTable = oSheet.CustomTables.Item(TableNr)

'Type of Media
Dim oMedia As String
'set the cell to edit, you can add numbers or name
'first cell to edit
oCell1= oCustomTable.Rows.Item(1).Item("Description")
'try and get it from the model
Try
    oMedia = Parameter(modelName, "MediaType")
'if not found, prompt user for it
Catch
    oMedia = InputBox("Contents", "iLogic", oCell1.Value)
End Try
'add media to table
oCell1.Value = oMedia

'Capacity
Dim oCapEst As String
'second cell we're editing
oCell2= oCustomTable.Rows.Item(2).Item("Description")
'try and get it from the model
Try
    oCapEst = Parameter(modelName, "CapEst") & " Litres"
'if not found, prompt user for it
Catch

    oCapEST = InputBox("Working Capcity (EST)", "iLogic", oCell2.Value)
End Try
'add Capacity to table
oCell2.Value = oCapEST

'Dry Weight
Dim oDryWgt As String
'3rd cell we're editing
oCell3 = oCustomTable.Rows.Item(3).Item("Description")
'try and get it from the model
Try
    oDryWgt = Parameter(modelName, "DryWgt") & " Kg"
'if not found, prompt user for it
Catch

    oDryWgt = InputBox("Dry Weight", "iLogic", oCell3.Value)
End Try
'add Weight to table
oCell3.Value = oDryWgt

'Total Weight
Dim oMaxWgt As String
'4th cell we're editing
oCell4= oCustomTable.Rows.Item(4).Item("Description")
'try and get it from the model
Try
    oMaxWgt = Parameter(modelName, "MaxWgt") & " Kg"
'if not found, prompt user for it
Catch

    oMaxWgt = InputBox("Max Working Weight", "iLogic", oCell4.Value)
End Try
'add total weight to table
oCell4.Value = oMaxWgt

'Specific Gravity of Media
Dim oSpecGrav As String
'5th cell to edit
oCell5= oCustomTable.Rows.Item(5).Item("Description")
'try and get it from the model
Try
    oSpecGrav = Parameter(modelName, "SpecGrav")
'if not found, prompt user for it
Catch
    oSpecGrav = InputBox("Specific Gravity", "iLogic", oCell5.Value)
End Try
'add Specific Gravity to table
oCell5.Value = oSpecGrav

'Material
Dim oMaterial As String
'6th cell to edit
oCell6= oCustomTable.Rows.Item(8).Item("Description")
'try and get it from the model
Try
    oMaterial = Parameter(modelName, "MaterialType")
'if not found, prompt user for it
Catch
    oMaterial = InputBox("Material", "iLogic", oCell6.Value)
End Try
'add material to table
oCell6.Value = oMaterial
'------------------------------------------------------
'---------------finish editing the table---------------
'------------------------------------------------------


EndRule: