ilogic to get part extents

ilogic to get part extents

schleede.dave
Enthusiast Enthusiast
5,862 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,863 Views
29 Replies
Replies (29)
Message 21 of 30

Owner2229
Advisor
Advisor

Hi, Justin.

 

First, call me just Mike. There's no need for nicknames, as we're like coleagues here, I believe.

 

I'm ussing (not always, sometimes I just grab what I have in my snipset) the reference keys as they're a bit faster. Sure, there's no need for them here as it only matters if it checks a lot of parts, but it's good to get them under the skin.

It improves the performance by about 2% as the reference key is accesing the reference directly while the enumerator has to be translated.

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 22 of 30

schleede.dave
Enthusiast
Enthusiast

It looks to me like your updated code prevents the error message by preventing the rest of the code from running at all.  I still have to open each part manually & trigger the code from within the part to get the info I want into iProperties.  Is there not a way to automate this?  Perhaps a code in the assembly that will open, save & close any parts that have been updated?

 

I now understand why the "New Document" event trigger didn't work.  Thank you for explaining that.  My thinking was that it would trigger when the "make components" command created the new part.  I now see that even if the trigger worked there wouldn't be any geometry in the part at that time so I wouldn't get any data for "length", "width" & "thickness".  Just a note that I also tried a "before save" trigger.  This worked, the ilogic code was triggered but then it generated the error I mentioned in my previous post.

 

Your post did lose me here:

 

"Owner2229 wrote:

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."

 

I am creating a multi-body part and using the make components command to create both the new assembly and parts.  When doing this the assembly and parts are automatically linked back to the original multi-body part and they already automatically update when the source part is changed/updated.  Why would I need a rule to do what the make components command is already doing?  I'm not understanding the logic to this.

 

I also don't understand why the rule would write iProperties from the source multibody part to the new parts created from it.  The new parts created by the "make components" command have their own geometry; why can't the data be pulled from there after the part is created?

 

I think the issue is that for the code we have now to work each of the parts has to be physically open (is this correct?).  I had thought that there was a way to make the code work without having to open the part.  I now realize I must be wrong.  I think you're suggesting an entirely different method of accomplishing this for that reason (at least I THINK it's entirely different!  I'm still a bit confused!).

 

Would it be possible to leave the code alone and to create a second code in the assembly that would just open, save & close any parts that have been updated/revised?

 

 

I really appreciate the help by the way.  iLogic is not where my skill set is.  It's something I'd love to learn more about when I have the time.

 

 

 

 

 

0 Likes
Message 23 of 30

Owner2229
Advisor
Advisor

Hi, I ment it just like you sayd. When you create the parts and assembly with the "make components cmd" and then change the original model it wont then update the parts and assembly. It's because you need a rule that will open them, update them and save them.

They would eventualy update, but you'd have to open them by your own and that's quite annoying.

But to open them you need to know their names and location.

So it's better to generate them by a rule and save somewhere their path and filename (it might be iProperties, Parameter or some sort of server database).

 

As for the iProperties, I ment is as the rule from the source part would generate the names, part numbers, descriptions, material settings (etc.) for the derived assembly and parts.

 

Yes, the parts/assy has to be opened in order to update them. I can be done on background, so Inventor wont need to generate the graphic for the part and thus it would be way faster.

 

Im bassicly suggesting the same what you have been doing so far, but do so by code instead of manual clicking.

 

It is possible to only open, save and close the parts and assy. But it leeds us back to their path and filenames.

 

So, the question is, how are your derived parts and assy named? Is the name some kind of derivation from the source part or is it entirely different / set by rules in your company or information system.

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 24 of 30

schleede.dave
Enthusiast
Enthusiast

Thanks for explaining.  Right now the part names are just coming from the solid bodies.

0 Likes
Message 25 of 30

Owner2229
Advisor
Advisor

Hi, you can give a try to something like this:

 

Sub Main()
Dim oDoc As Document = ThisDoc.Document 
Dim oDef As ComponentDefinition = oDoc.ComponentDefinition

' Assume the derived parts are stored in the same folder
' as the source part Dim oPath As String = ThisDoc.Path & "\"
' Go throught all surface bodies
Dim aDoc As Document
Dim oSB As SurfaceBody For Each oSB In oDef.SurfaceBodies ' Assume the file name is the same as the name of the surface body
Try
aDoc = ThisApplication.Documents.Open(oPath & oSB.Name & ".ipt", False)
UpdatePart(aDoc)
aDoc.Save
aDoc.Close(True)
Catch
End Try Next End Sub Private Sub UpdatePart(oDoc As Document) ' 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 End Sub
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 26 of 30

schleede.dave
Enthusiast
Enthusiast

Thank you.  I tried your code but it isn't working.  Either that or I am using it incorrectly.

 

I added a rule containing your code to the multi-body solid.  After using the "make components" command to create the part files I ran the rule.  It gives me the error "This model cannot be converted to Sheet Metal becuase it contains features that have created multiple bodies.  In order to convert to sheet metal these features must first be removed".  It gives me this error once for every part that was created.  If I keep clicking "OK" it keeps running until its gone through all the parts.  It appears to be creating the flat pattern in each part but it is not generating the length, width or thickness parameters.

0 Likes
Message 27 of 30

Boorda
Advocate
Advocate

 

I think you could really simplify this part of your code:

 

'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)

 Above you are doing a lot of conversions when really all you need to do is pull your Length & Widths from the FlatPattern object like this.

 

'Define the Flat Pattern variables.
Dim SCdef as SheetMetalComponentDefinition, FP As FlatPattern, UOM as UnitsOfMeasure, Width As String, Length As String

'Set the objects.
Set SCdef = ThisApplication.ActiveDocument.ComponentDefinition
Set FP = SCdef.FlatPattern

'Convert the values to units set in document.
Width = UOM.GetStringFromValue(FP.Width, UOM.LengthUnits)

'Convert the values to units set in document.
Length = UOM.GetStringFromValue(FP.Length, UOM.LengthUnits)



Also if you are converting a SolidPart to Sheet Metal Part then you will get a "Thickness" parameter, so you could just pull your value from there.

When converting from a solid part you will usually need to set your sheet metal default and modify the previous solid parts parameter to use the new Thickness parameter, if you do not then you will not be able to unfold models.

 

 

 


Automation is key!
0 Likes
Message 28 of 30

schleede.dave
Enthusiast
Enthusiast

I tried your code but it isn't working.  It gives me an error.  Did you test it and have it working?

 

Also, converting a solid body part to sheet metal does give you a "thickness" parameter but the value of the parameter comes from your sheet metal defaults, not the actual part thickness.

0 Likes
Message 29 of 30

Boorda
Advocate
Advocate

Sorry for the late response.

Yes I have tested an use the method I posted.

I would need to see your updated macro to understand where your error is coming from.

 

Also, you are correct, the Thickness is controled by the sheet metal defaults, but you will notice that all of the sheet metal defaults are saved as parameters.

Therefore you can access whatever parameter is driving your parts thickness and link it to the sheet metal thickness and pull the value from there.

 

 

You can post or PM me the code if you want me to have a go at it.

 

-Addam


Automation is key!
0 Likes
Message 30 of 30

Anonymous
Not applicable

先生你好,

看了你们讨论的规则,我有一个疑问想请教一下。新生成的板金零部件如何让他们都默认自带一个展开的规则?怎样设置这个默认的规则?

我希望可以直接从模型中自动获得对应的长宽参数。

 

0 Likes