ilogic to get part extents

ilogic to get part extents

schleede.dave
Enthusiast Enthusiast
5,828 Views
29 Replies
Message 1 of 30

ilogic to get part extents

schleede.dave
Enthusiast
Enthusiast
I need an iLogic routine that returns the length, width and thickness extents of a
part created from a solid body model using the "make components" command. I know
that Inventor has sheet metal functions for getting the X & Y values of a sheet
metal part from its flat pattern so I started there by converting my standard part
to sheetmetal & then creating a flat pattern. This seems to work fine for getting
the Length & the Width of the part but I don't know how to get an accurate thickness.

My first thought was to use iProperties.Volume and then divide that value by
SheetMetal.FlatExtentsArea but that only works if the part is square/rectangular and
has no cutouts or holes. Is there a way to get the actual thickness from the flat
pattern or elsewhere? Keep in mind the parts I'm dealing with were created with Make
Components so they don't have parameters that I can pull the width from.

Assuming someone can help me with the Thickness issue I would also like to know how
to exit the flat pattern at the end. I might even want to exit the flat pattern,
delete it and turn the part back into a standard part (that may be unnecessary).
Any help with this would also be appreciated.

Anyhoo, here is my code thus far:

'
The purpose of this iLogic is to get length, width and thickness values for parts
that are created from
'multi-body solid models using the "Make Part" Or "Make Components"
command. This is done by converting
'the standard parts to sheet metal, creating
flat patterns and using the SheetMetal.FlatExtents commands
'Checks to see if the active document is a standard part. If ThisApplication.ActiveDocument.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" 'If it is a standard part it is converted to sheet metal Try ThisApplication.ActiveDocument.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Catch 'catch error and exit rule when part can't be converted 'example: multiple solid body part Return End Try End If 'Gets the document's units of measure Dim uom As UnitsOfMeasure = ThisApplication.ActiveDocument.UnitsOfMeasure 'Converts length unit to a string Dim Units As String = uom.GetStringFromType(uom.LengthUnits) 'Gets the X & Y dimensions of the part from sheetmetal extents X=SheetMetal.FlatExtentsLength Y=SheetMetal.FlatExtentsWidth

'****** This is where I'm trying to calculate the thickness ****** 'Calculates the Z dimension of the part using part volume and sheetmetal extents.
'Might be a better way to do this but this is was my best idea at the time.
'Not such a good idea after all as it only works if the part is square/rectangular and has no holes!!! Z=iProperties.Volume/SheetMetal.FlatExtentsArea Dim LengthString As String = MaxOfMany (X,Y,Z) Dim WidthString As String = X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z) Dim ThicknessString As String = MinOfMany (X,Y,Z) 'Sets iProperties for Length, Width & Thickness. Unit string is added to each value.
'No formatting is done to these values and all the decimal places are left in place.
'My thought here is that these values are to be used in the BOM only and formatting can be handled by the parts list settings. iProperties.Value("Custom", "Length") = LengthString & " " & Units iProperties.Value("Custom", "Width") = WidthString & " " & Units iProperties.Value("Custom", "Thickness") = ThicknessString & " " & Units 'Leave FlatPattern and return to Folded model'***I don't know how to do this*** 'ThisDoc.Save   

 

0 Likes
Accepted solutions (1)
5,829 Views
29 Replies
Replies (29)
Message 2 of 30

Owner2229
Advisor
Advisor

Here you go:

 

' Get active document
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

' Checks to see if the active document is a standard part.
If oDoc.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" ' If it is a standard part it is converted to sheet metal Try oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Catch ' Catch error and exit rule when part can't be converted ' Example: multiple solid body part Return End Try End If
' Gets the document's units of measure Dim uom As UnitsOfMeasure = oDoc.UnitsOfMeasure ' Converts length unit to a string Dim Units As String = uom.GetStringFromType(uom.LengthUnits) ' Gets the X & Y dimensions of the part from sheetmetal extents X = SheetMetal.FlatExtentsLength Y = SheetMetal.FlatExtentsWidth

' Go to the FlatPattern
Dim oSMCD As SheetMetalComponentDefinition
oSMCD = oDoc.ComponentDefinition
' Look for Flatpattern
If Not oSMCD.HasFlatPattern Then
' Create Flatpattern if the part doesn't have one
oSMCD.Unfold()
oDoc.Update2(True)
Else
oSMCD.FlatPattern.Edit
End If

' Get the min and max point of the Flatpattern
Dim minPoint As point = oSMCD.SurfaceBodies.Item(1).RangeBox.minPoint Dim maxPoint As point = oSMCD.SurfaceBodies.Item(1).RangeBox.maxPoint

' Tickness is maxPoin - minPoint
' Since FlatPattern always returns "cm", we have to multiple it by 10 to get "mm"
Z = (maxPoint.Z - minPoint.Z) * 10

' Create measure strings Dim LengthString As String = MaxOfMany (X,Y,Z) Dim WidthString As String = X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z) Dim ThicknessString As String = MinOfMany (X,Y,Z) ' Sets iProperties for Length, Width & Thickness. Unit string is added to each value. iProperties.Value("Custom", "Length") = LengthString & " " & Units iProperties.Value("Custom", "Width") = WidthString & " " & Units iProperties.Value("Custom", "Thickness") = ThicknessString & " " & Units

' Exit the FlatPattern
oSMCD.FlatPattern.ExitEdit()

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 30

schleede.dave
Enthusiast
Enthusiast

Thank you very much!  That works perfectly!!!

0 Likes
Message 4 of 30

schleede.dave
Enthusiast
Enthusiast

Actually it looks like I spoke too soon.  I have several cases where it does not work.  The Z value ends up being larger than both the X & Y.  In one case I have a plate that is 6" X 3" X .025" and I end up with an X of ~152, a Y of 6 and a Z of 3.  Looking into it more now.

0 Likes
Message 5 of 30

schleede.dave
Enthusiast
Enthusiast

Here's what's happening:  Inventor is coming up with a Maxpoint.z of 7.62 and a Minpoint.z of -7.62.

Z = (maxpoint.Z - minPoint.Z) * 10 = 152.4 which is the value I end up getting for length on this particular part.

 

I thought it worked perfectly on the first few parts I tried it on but I was mistaken; after reviewing them all I've found that each of them has an error with either the thickness or the length.  The width always seems to come out correct.

 

The first issue is that the Z that iLogic calculates isn't always the thickness, sometimes it's actually the length.  I think this is solveable by eliminating the lines:

X=SheetMetal.FlatExtentsLength
Y=SheetMetal.FlatExtentsWidth

 

 

and using these two lines instead:

X=(maxPoint.X - minPoint.X) * 10
Y=(maxPoint.Y - minPoint.Y) * 10

With that change there should be no repeating. 

 

 

The second issue is one that should've been obvious to me as you pointed it out right in your code.  FlatPattern always returns CM.  The parts I'm testing this on are in inches but we will also need this to work with mm.  I'm already checking the docments current unit of measure, I just need a way to make the X, Y & Z match this.  Is there a simply way of doing this?  Right now all that comes to mind is using a bunch of If-Then's.

 

0 Likes
Message 6 of 30

Curtis_Waguespack
Consultant
Consultant

HI schleede.dave,

I nly glanced at this, but I seem to recall that work planes and axes can cause this to come out wrong if they are visible, just something to look into, I'm not saying that's what's going on.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 7 of 30

schleede.dave
Enthusiast
Enthusiast

That is not the issue here but I thank you for your suggestion: it's something to keep in mind.

 

So far this seems to be working:

 

'Get active document
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

'Checks to see if the active document is a standard part.
If ThisApplication.ActiveDocument.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}"
'If it is a standard part it is converted to sheet metal
 Try
  ThisApplication.ActiveDocument.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
  Catch
  'catch error and exit rule when part can't be converted
  'example: multiple solid body part
  Return
  End Try
End If

'Go to the FlatPattern
Dim oSMCD As SheetMetalComponentDefinition
oSMCD = oDoc.ComponentDefinition
'Look for Flatpattern
If Not oSMCD.HasFlatPattern Then
'Create Flatpattern if the part doesn't have one
   oSMCD.Unfold()
   oDoc.Update2(True)
Else
   oSMCD.FlatPattern.Edit
End If
'Get the min and max point of the Flatpattern
Dim minPoint As point = oSMCD.SurfaceBodies.Item(1).RangeBox.minPoint
Dim maxPoint As point = oSMCD.SurfaceBodies.Item(1).RangeBox.maxPoint
'X, Y & Z values are maxPoint - minPoint
X=(maxPoint.X - minPoint.X)
Y=(maxPoint.Y - minPoint.Y)
Z=(maxPoint.Z - minPoint.Z)

'Since FlatPattern always returns "cm", we have to do some conversion to match the document's unit of measure'Gets the document's units of measure
Dim uom As UnitsOfMeasure = oDoc.UnitsOfMeasure
'Converts length unit to a string
Dim Units As String = uom.GetStringFromType(uom.LengthUnits)

If Units = "meter"
X = Round(X * 0.01, 3)
Y = Y * 0.01
Z = Z * 0.01
Else If Units = "foot"
X = X * 0.0328084
Y = Y * 0.0328084
Z = Z * 0.0328084
Else If Units = "inch"
X = Round(X * 0.393708,3)
Y = Y * 0.393708
Z = Z * 0.393708
Else If Units = "mm"
X = X * 10
Y = Y * 10
Z = Z * 10
ElseIf Units = "micron"
X = X * 10000
Y = Y * 10000
Z = Z * 10000
End If

Dim LengthString As String = MaxOfMany (X,Y,Z)
Dim WidthString As String = X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z)
Dim ThicknessString As String = MinOfMany (X,Y,Z)

'Sets iProperties for Length, Width & Thickness.  Unit string is added to each value.

iProperties.Value("Custom", "Length") = LengthString & " " & Units
iProperties.Value("Custom", "Width") = WidthString & " " & Units
iProperties.Value("Custom", "Thickness") = ThicknessString & " " & Units

'Exit the FlatPattern
oSMCD.FlatPattern.ExitEdit()

'ThisDoc.Save
 

 

0 Likes
Message 8 of 30

schleede.dave
Enthusiast
Enthusiast

With a little more reading I found a better way to handle the unit conversion.  This seems to be working fine so far:

 

'Get active document
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument
'Get active document unit of measure
Dim uom As UnitsOfMeasure = oDoc.UnitsOfMeasure
'Converts length unit to a string
Dim Units As String = uom.GetStringFromType(uom.LengthUnits)

'Checks To see If the active document Is a standard part.
If ThisApplication.ActiveDocument.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}"
'If it Is a standard part it Is converted To sheet metal
 Try
  ThisApplication.ActiveDocument.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
  Catch
  'catch error and exit rule when part can't be converted
  'example: multiple solid body part
  Return
  End Try
End If

'Go To the FlatPattern
Dim oSMCD As SheetMetalComponentDefinition
oSMCD = oDoc.ComponentDefinition
'Look For Flatpattern
If Not oSMCD.HasFlatPattern Then
'Create Flatpattern If the part doesn't have one
   oSMCD.Unfold()
   oDoc.Update2(True)
Else
   oSMCD.FlatPattern.Edit
End If

'Get the min and max point of the Flatpattern
Dim minPoint As point = oSMCD.SurfaceBodies.Item(1).RangeBox.minPoint
Dim maxPoint As point = oSMCD.SurfaceBodies.Item(1).RangeBox.maxPoint
'X, Y & Z values are maxPoint - minPoint'Since FlatPattern always returns "cm", we also have to do some conversion to match the document's unit of measure
X = uom.ConvertUnits ((maxPoint.X - minPoint.X), "cm", uom.LengthUnits)
Y = uom.ConvertUnits ((maxPoint.Y - minPoint.Y), "cm", uom.LengthUnits)
Z = uom.ConvertUnits ((maxPoint.Z - minPoint.Z), "cm", uom.LengthUnits)

Dim LengthString As String = MaxOfMany (X,Y,Z)
Dim WidthString As String = X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z)
Dim ThicknessString As String = MinOfMany (X,Y,Z)

'Set iProperties for Length, Width & Thickness.  Unit string added to each value.
iProperties.Value("Custom", "Length") = LengthString & " " & Units
iProperties.Value("Custom", "Width") = WidthString & " " & Units
iProperties.Value("Custom", "Thickness") = ThicknessString & " " & Units
'iProperties.Value("Project", "Description") = "PL - " & ThicknessString & " " & Units

'Exit the FlatPattern
oSMCD.FlatPattern.ExitEdit()

'ThisDoc.Save

 

0 Likes
Message 9 of 30

MechMachineMan
Advisor
Advisor

It might be worth trying, but I think you can speed it up slightly by not editting the flat pattern; just access the component definition of the flat pattern by using

 

Dim oFPCompDef As ComponentDefinition = oSheetMetalCompDef.FlatPattern


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 10 of 30

schleede.dave
Enthusiast
Enthusiast

Can you show me where I'd put that in my code?

0 Likes
Message 11 of 30

Owner2229
Advisor
Advisor
Accepted solution

Hi, here below is your code with the change that Justin suggested, but you should test it first.

 

'Get active document
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument
'Get active document unit of measure
Dim uom As UnitsOfMeasure = oDoc.UnitsOfMeasure
'Converts length unit to a string
Dim Units As String = uom.GetStringFromType(uom.LengthUnits)

'Checks To see If the active document Is a standard part.
If ThisApplication.ActiveDocument.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}"
'If it Is a standard part it Is converted To sheet metal
   Try
      ThisApplication.ActiveDocument.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
   Catch
      'catch error and exit rule when part can't be converted
      'example: multiple solid body part
      Return
   End Try
End If

'Go To the FlatPattern
Dim oSMCD As SheetMetalComponentDefinition
oSMCD = oDoc.ComponentDefinition
'Look For Flatpattern
If Not oSMCD.HasFlatPattern Then
   'Create Flatpattern If the part doesn't have one
   oSMCD.Unfold()
   oDoc.Update2(True)
   oSMCD.FlatPattern.ExitEdit()
End If

'create the component definition of Flatpattern
Dim oFPCD As ComponentDefinition = oSMCD.FlatPattern 'Get the min and max point of the Flatpattern Dim minPoint As point = oFPCD.SurfaceBodies.Item(1).RangeBox.minPoint Dim maxPoint As point = oFPCD.SurfaceBodies.Item(1).RangeBox.maxPoint 'X, Y & Z values are maxPoint - minPoint'Since FlatPattern always returns "cm", we also have to do some conversion to match the document's unit of measureX = uom.ConvertUnits ((maxPoint.X - minPoint.X), "cm", uom.LengthUnits) Y = uom.ConvertUnits ((maxPoint.Y - minPoint.Y), "cm", uom.LengthUnits) Z = uom.ConvertUnits ((maxPoint.Z - minPoint.Z), "cm", uom.LengthUnits) Dim LengthString As String = MaxOfMany (X,Y,Z) Dim WidthString As String = X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z) Dim ThicknessString As String = MinOfMany (X,Y,Z) 'Set iProperties for Length, Width & Thickness. Unit string added to each value. iProperties.Value("Custom", "Length") = LengthString & " " & Units iProperties.Value("Custom", "Width") = WidthString & " " & Units iProperties.Value("Custom", "Thickness") = ThicknessString & " " & Units 'iProperties.Value("Project", "Description") = "PL - " & ThicknessString & " " & Units 'ThisDoc.Save
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 12 of 30

MechMachineMan
Advisor
Advisor

Don't forget to comment out/remove the ExitEdit line though!

 

'Get active document
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument
'Get active document unit of measure
Dim uom As UnitsOfMeasure = oDoc.UnitsOfMeasure
'Converts length unit to a string
Dim Units As String = uom.GetStringFromType(uom.LengthUnits)

'Checks To see If the active document Is a standard part.
If ThisApplication.ActiveDocument.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}"
'If it Is a standard part it Is converted To sheet metal
   Try
      ThisApplication.ActiveDocument.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
   Catch
      'catch error and exit rule when part can't be converted
      'example: multiple solid body part
      Return
   End Try
End If

'Go To the FlatPattern
Dim oSMCD As SheetMetalComponentDefinition
oSMCD = oDoc.ComponentDefinition
'Look For Flatpattern
If Not oSMCD.HasFlatPattern Then
   'Create Flatpattern If the part doesn't have one
   oSMCD.Unfold()
   oDoc.Update2(True)
   'oSMCD.FlatPattern.ExitEdit()
End If

'create the component definition of Flatpattern
Dim oFPCD As ComponentDefinition = oSMCD.FlatPattern

'Get the min and max point of the Flatpattern
Dim minPoint As point = oFPCD.SurfaceBodies.Item(1).RangeBox.minPoint
Dim maxPoint As point = oFPCD.SurfaceBodies.Item(1).RangeBox.maxPoint
'X, Y & Z values are maxPoint - minPoint'Since FlatPattern always returns "cm", we also have to do some conversion to match the document's unit of measureX = uom.ConvertUnits ((maxPoint.X - minPoint.X), "cm", uom.LengthUnits)
Y = uom.ConvertUnits ((maxPoint.Y - minPoint.Y), "cm", uom.LengthUnits)
Z = uom.ConvertUnits ((maxPoint.Z - minPoint.Z), "cm", uom.LengthUnits)

Dim LengthString As String = MaxOfMany (X,Y,Z)
Dim WidthString As String = X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z)
Dim ThicknessString As String = MinOfMany (X,Y,Z)

'Set iProperties for Length, Width & Thickness.  Unit string added to each value.
iProperties.Value("Custom", "Length") = LengthString & " " & Units
iProperties.Value("Custom", "Width") = WidthString & " " & Units
iProperties.Value("Custom", "Thickness") = ThicknessString & " " & Units
'iProperties.Value("Project", "Description") = "PL - " & ThicknessString & " " & Units

 

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 13 of 30

schleede.dave
Enthusiast
Enthusiast

Thank you both, that works very well.  It actually works with the ExitEdit line left active; is there a reason you thought this line should be commented out?  I kind of like that it exits the flat and goes back to the "folded" part.

0 Likes
Message 14 of 30

MechMachineMan
Advisor
Advisor
I guess it depends how you start your rule. Without calling edit/exit edit,
you can actually fetch all of the information about the flat pattern from
the folded model by just using the .FlatPattern to fetch only the unfolded
models component definition.

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 15 of 30

schleede.dave
Enthusiast
Enthusiast

In other words the code provided by Owner2229 could be edited further?  Can you show me what you mean by editing the code?

0 Likes
Message 16 of 30

MechMachineMan
Advisor
Advisor

Sorry; it would appear I was mistaken. My code that I used actually had a bug that got overlooked because of sloppy error handling.

 

Thanks for the actual correction!

 

ExitEdit is needed as it is inherent in the unfold method that it swaps to the unfolded part.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 17 of 30

schleede.dave
Enthusiast
Enthusiast

I have a couple other question regariding this iLogic code.  In my testing I created a multibody solid that I called "Test.ipt".  The multibody solid contains 4 solids that are named solid1, solid2, etc.  I used the make components command to create ipts for each of the solids as well as an assembly called "test.iam.  I added a "new document" event trigger to the part template used by the make components command but that didn't seem to work; if I right click any of the parts in the test.iam assembly and check for iProperties they are blank.  To get the length, width and thickness iProperties of the parts I have to open each of them and run the ilogic manually.  Is there a way to make this happen automatically?

 

I've also added a part geometry change event trigger to the template used by the make components command.  This trigger works fine I have the part open and make a change to the part itself.  However, if I make a change in the Test.ipt (the original multibody solid) and then update Test.iam (the linked assembly created by make components command) I get the following error:

 

Error in rule: Solid Body Component - Get Measurements, in document: Solid1.ipt

Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.SheetMetalComponentDefinition'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{0562B816-F05F-4293-AF39-D2F640E42740}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

Is there a way to fix this as well?  Or do I have to remove both even triggers and run the routine manually on each part every time I make a change?

0 Likes
Message 18 of 30

schleede.dave
Enthusiast
Enthusiast

Anyone?  I'm still struggling with this.

0 Likes
Message 19 of 30

Owner2229
Advisor
Advisor

Hi, as for the multibody to assembly, you should use a rule in the part, that will generate both the new parts and assembly and remember theyr names and path, so it can replace/update them when the source part is changed/updated. The rule also has to write in the iProperties from the source part to the assembly and parts, as the assembly / parts can't get the iProperties from nowhere.

 

You shouldn't use "New document" event trigger, as it will run only once, when you create new part and start modeling. So at this point there is nothing to generate. You can use "After save" event trigger, but only if you don't use the template for something where you don't waht to run it. The optimal state would be having the rule in the template with set up "After save" event trigger, but the will be dissabled, so it wont run before you enable it. After you first enable it, it should then update all it's derived parts and assembly after each save (thus intended change) of the source part.

 

I hope it's clear enough.

 

Here below is corrected rule, so it wont throw errors in assembly, multi-body or in whatever else is your part attempting to run it.

 

 

'Get active document
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument
'Get active document unit of measure
Dim uom As UnitsOfMeasure = oDoc.UnitsOfMeasure
'Converts length unit to a string
Dim Units As String = uom.GetStringFromType(uom.LengthUnits)

'Checks To see If the active document Is a standard part
If oDoc.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then
'If it Is a standard part we convert it to sheet metal
   Try
      oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
   Catch
      'catch error and exit rule when part can't be converted
      'example: multiple solid body part
      Return
   End Try
End If

'Check if the part is sheet metal now
If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
   'Go To the FlatPattern
   Dim oSMCD As SheetMetalComponentDefinition
   oSMCD = oDoc.ComponentDefinition
   'Look For Flatpattern
   If Not oSMCD.HasFlatPattern Then
      'Create Flatpattern If the part doesn't have one
      oSMCD.Unfold()
      oDoc.Update2(True)
      oSMCD.FlatPattern.ExitEdit()
   End If

   'create the component definition of Flatpattern
   Dim oFPCD As ComponentDefinition = oSMCD.FlatPattern

   'Get the min and max point of the Flatpattern
   Dim minPoint As point = oFPCD.SurfaceBodies.Item(1).RangeBox.minPoint
   Dim maxPoint As point = oFPCD.SurfaceBodies.Item(1).RangeBox.maxPoint
   'X, Y & Z values are maxPoint - minPoint'Since FlatPattern always returns "cm", we also have to do some conversion to match the document's unit of measureX = uom.ConvertUnits ((maxPoint.X - minPoint.X), "cm", uom.LengthUnits)
   Dim Y As Double = uom.ConvertUnits ((maxPoint.Y - minPoint.Y), "cm", uom.LengthUnits)
   Dim Z As Double = uom.ConvertUnits ((maxPoint.Z - minPoint.Z), "cm", uom.LengthUnits)

   Dim LengthString As String = MaxOfMany (X,Y,Z)
   Dim WidthString As String = X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z)
   Dim ThicknessString As String = MinOfMany (X,Y,Z)

   'Set iProperties for Length, Width & Thickness.  Unit string added to each value.
   iProperties.Value("Custom", "Length") = LengthString & " " & Units
   iProperties.Value("Custom", "Width") = WidthString & " " & Units
   iProperties.Value("Custom", "Thickness") = ThicknessString & " " & Units
   'iProperties.Value("Project", "Description") = "PL - " & ThicknessString & " " & Units

End If

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 20 of 30

MechMachineMan
Advisor
Advisor
Owner, I'm curious as to why you use the reference keys rather than the
plain English enumerator strings. Can you explain please?

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes