Thursday 15 September 2016

Convert Parts to Sheet Metal

Still slow on picking up old habits and I am ridiculously busy at work. I think they wouldn’t mind if I get a bed in the office and move in completely.

The other day I started investigating how to convert a batch of parts to sheet metal? If you have a lot of files you might want to use code injector from here. This will insert the code in the files, run it and then remove it from the files cleaning them up.

layers upon layers..



The code to use is.

---------------------------------------------
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
 
'Change Document.SubType to Sheetmetal
oPartDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"

----------------------------------------------

But what if have an assembly open and I want to convert specific parts? This code will allow you change parts to sheet metal from within the assembly. As long as you don't press ESC and you keep selecting parts in the graphical window it will convert them to sheet metal parts.


 ----------------------------------------------

Dim oADoc as AssemblyDocument
oADoc = ThisApplication.ActiveDocument
 
Dim comp As Object
 
While True
    'prompt user to select an occurrence
    comp = ThisApplication.CommandManager.Pick(
        SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, 
        "Select a component") 
    If comp Is Nothing Then
        MessageBox.Show ("Selection was cancelled","ilogic")
        Beep
        Exit While
    End If
    
    oPartDoc = comp.Definition.Document
    
    Try
        oPartDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
    Catch
        MessageBox.Show("Some error occurred, make sure it's not read-only or library item","error")
    End Try
 
End While

----------------------------------------------
Here’s a short animation.


convert to sheet metal

And then you might ask how to change thickness while we’re here?
This presented a problem in itself because the thickness parameter can’t be accessed directly and the end value seemed to be 10x bigger..!
I read somewhere that the default Inventor units are cm and I needed them in mm but don’t take my word for it.

Here is the code with prompted thickness value.

 ----------------------------------------------
Dim oADoc as AssemblyDocument
oADoc = ThisApplication.ActiveDocument
 
Dim comp As Object
 
 
While True
    'prompt user to select an occurrence
    comp = ThisApplication.CommandManager.Pick(
        SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, 
        "Select a component") 
    If comp Is Nothing Then
        MessageBox.Show ("Selection was cancelled","ilogic")
        Beep
        Exit While
    End If
    
    oPartDoc = comp.Definition.Document
    
    Try
        oPartDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
        
        Dim oSheetMetalCompDef As SheetMetalComponentDefinition
        oSheetMetalCompDef = oPartDoc.ComponentDefinition
        
        ' Override the thickness for the document
        oSheetMetalCompDef.UseSheetMetalStyleThickness = False
        
        ' Get a reference to the parameter controlling the thickness.
        Dim oThicknessParam As Parameter
        oThicknessParam = oSheetMetalCompDef.Thickness
                
        
        'promot user to enter thickness
        'this will get current and ask for new one
        Dim oNewTHK As Double
        'read that default units are cm need to get them to mm
        'but I suspect this is to do with not pecifying explicitly units
        oNewTHK = InputBox("Enter Thickness", "iLogic",oThicknessParam.Value) 
        ' Change the value of the parameter.
        oThicknessParam.Value = oNewTHK/10
    Catch
        MessageBox.Show("Some error occurred, make sure it's not read-only or library item","error")
        Exit While
    End Try
 
End While

----------------------------------------------

Later,
ADS

photo credit: DC-3 (license)

No comments:

Post a Comment