Polygon area in a sketch to be printed as parameter

Polygon area in a sketch to be printed as parameter

RStancescu
Collaborator Collaborator
1,064 Views
16 Replies
Message 1 of 17

Polygon area in a sketch to be printed as parameter

RStancescu
Collaborator
Collaborator

Hello all Inventor gurus

I'm in a 2D sketch and have a polygon, could be a square, rectangle, etc. I know I can measure its area with Region Properties. How can I retain that measured area value?  

I need that area number to be used later in a formula. 

Thank you

Radu

 

 

I'm using Inventor 2022. 

0 Likes
1,065 Views
16 Replies
Replies (16)
Message 2 of 17

johnsonshiue
Community Manager
Community Manager

Hi Radu,

 

Regarding retaining the area property, I believe this will need help from iLogic. Inventor API should have calls to get the area property from a selected profile. Then you can push this value to a user parameter in area unit (in^2 or mm^2). Here is the example.

 

https://adndevblog.typepad.com/manufacturing/2015/09/querying-a-sketch-profile-to-get-regions.html

 

Many thanks!



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
Message 3 of 17

gcoombridge
Advisor
Advisor

Something like this (Assumes profile is by itself in Sketch1) - also assumes you are talking about the part environment:

Dim oDoc As PartDocument = ThisDoc.Document
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim oParam As UserParameter
Dim oProfile As Profile

'Find the sketch
	Dim oSketch As PlanarSketch = oDef.Sketches.Item("Sketch1")
	'Get the profile from the sketch - should only contain one
Try
		oProfile = oSketch.Profiles.AddForSolid
		MsgBox(Round(oProfile.RegionProperties.Area * 100, 2) & " mm^2", , "Area of Polygon")
		
			'Test whether parameter exists
			Try
				Parameter.Value("PolygonArea") = Round(oProfile.RegionProperties.Area * 100, 2)
			Catch 'Doesn't exist - then make it
				oParam = oDef.Parameters.UserParameters.AddByExpression("PolygonArea", Round(oProfile.RegionProperties.Area * 100, 2), "mm^2")
			End Try
Catch
		MsgBox("No Profile Found!", , "Warning")
End Try
Use iLogic Copy? Please consider voting for this long overdue idea (not mine):https://forums.autodesk.com/t5/inventor-ideas/string-replace-for-ilogic-design-copy/idi-p/3821399
Message 4 of 17

RStancescu
Collaborator
Collaborator

Hi Johnson,

How can I use that script to select a polygon and write the area to a parameter?

I assume that script needs to be put in API and not an ilogic rule.

Please advice

radu

0 Likes
Message 5 of 17

RStancescu
Collaborator
Collaborator

Thank you for your reply.

I copied that in a rule and I see how the total area is added to a parameter. Is there a way to accomplish the following:

- If I have three different polygons, when I run the rule, could it ask me to select a profile and then link that to a parameter? So in the end, I will have three different parameters linked with three polygons. 

Eventually it should ask me to select the area until I close it. 

 

After that, I can create another parameter that adds some parameters or else. 

 

Right now, it's calculating all closed contours in the sketch and adds them to one parameter.

 

I also voted for the idea in your signature, that looks very ineteresting!

 

Thank you for all your help

radu

Message 6 of 17

gcoombridge
Advisor
Advisor

@RStancescu I've tweaked it to do most of what you asked. Just note you used the work linked - these parameters will not be linked to the areas of the polygons, just fed the area of them by the rule. In the first rule the area could be effectively linked because you could set an event trigger to run on save. With user input tasks like the pick in the following rule you won't want to do that - it will drive users crazy!

 

Dim oDoc As PartDocument = ThisDoc.Document
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim oParam As UserParameter
Dim oProfile As Profile

	Dim oObjCol As ObjectCollection
	oObjCol = ThisApplication.TransientObjects.CreateObjectCollection()

Dim i As Integer = 1

'Find the sketch
	Dim oSketch As PlanarSketch = oDef.Sketches.Item("Sketch1")
	oSketch.Edit()
	'Get the profile from the sketch - should only contain one

	While True
	oProfile = ThisApplication.CommandManager.Pick(kSketchProfileFilter, "Select Profile (Press ESC to Exit)")
		If IsNothing(oProfile) Then Exit While
		oObjCol.Add(oProfile)	
		End While
		
					For Each oProfile In oObjCol
				
					MsgBox(Round(oProfile.RegionProperties.Area * 100, 2) & " mm^2", , "Area of Polygon " & i)
					
						'Test whether parameter exists
						Try
							Parameter.Value("PolygonArea" & i) = Round(oProfile.RegionProperties.Area * 100, 2)
						Catch 'Doesn't exist - then make it
							oParam = oDef.Parameters.UserParameters.AddByExpression("PolygonArea" & i, Round(oProfile.RegionProperties.Area * 100, 2), "mm^2")
						End Try
						i = i + 1
					Next oProfile

	oSketch.ExitEdit

 

One think you might also consider is making  boundary patch of each profile and then calculating the area of that. The advantage there is it is a named entity where as a sketch profile cannot be...

 

Use iLogic Copy? Please consider voting for this long overdue idea (not mine):https://forums.autodesk.com/t5/inventor-ideas/string-replace-for-ilogic-design-copy/idi-p/3821399
Message 7 of 17

mcgyvr
Consultant
Consultant

While ilogic will certain work.. Is it really needed for your situation? (just checking if simple will work)

mcgyvr_0-1633438644843.png

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 8 of 17

gcoombridge
Advisor
Advisor

@mcgyvr makes a good point! I had just assumed by polygon you meant >4 sides or complex shapes...

Use iLogic Copy? Please consider voting for this long overdue idea (not mine):https://forums.autodesk.com/t5/inventor-ideas/string-replace-for-ilogic-design-copy/idi-p/3821399
Message 9 of 17

RStancescu
Collaborator
Collaborator

@mcgyvr @gcoombridge 

I wish would be that simple. I have some irregular polygons that cannot provide areas by using formulas. They come from intersection of other profiles.

Radu

 

0 Likes
Message 10 of 17

RStancescu
Collaborator
Collaborator

It looks very good, thank you for your help!

I need to test it more and might come back with a couple more questions; I'm super busy now teaching/attending at AU 2021.

Appreciate it

Radu

 

0 Likes
Message 11 of 17

Stakin
Collaborator
Collaborator

there is a way to access your demand,wait for a while.

I post it for you.

0 Likes
Message 12 of 17

Stakin
Collaborator
Collaborator

There is a sketch named “sk1”

Stakin_0-1633583640527.png

To run the rule,it will create a parameter named “sk1_Area”,the unit is "mm^2"

Stakin_2-1633584050268.png

The area is the same as extrude feature end face by this sketch.

Stakin_3-1633584206861.png

 

 

 

 

 

 

Sub main
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
Dim oDocDef As PartComponentDefinition
oDocDef = oDoc.ComponentDefinition
Dim oSketch As PlanarSketch
Dim oSkName As String="sk1"
oSketch = oDocDef.Sketches.Item(oSkName)
Dim oProfile As Profile
oProfile = oSketch.Profiles.AddForSolid
Dim oProRegionProperties As RegionProperties
oProRegionProperties = oProfile.RegionProperties
Dim oAreaParaName As String
oAreaParaName = oSkName & "_Area"

Dim oAreaMM2 As Double
oAreaMM2=oProRegionProperties.Area
Dim oAreaPara As UserParameter
Try
	oAreaPara = oDocDef.Parameters.UserParameters.Item(oAreaParaName)
	oAreaPara.Units = "mm mm"
	oAreaPara.Value=oAreaMM2
Catch	
	oAreaPara=oDocDef.Parameters.UserParameters.AddByValue(oAreaParaName,oAreaMM2,"mm mm")
End Try

End Sub

 

 

 

 

hope to meet what you want;you should run the code every time the sketch is changed manually;Or you set a trigger to run it on file save.

Message 13 of 17

Stakin
Collaborator
Collaborator

Why can't I pick one Profile?

0 Likes
Message 14 of 17

gcoombridge
Advisor
Advisor

@Stakin you can - you just need to hit escape to exit the while. The tooltip should say that but they are cropping words in the last few versions. From my reading of the OP's requirements a number of different profiles in the same sketch need to be selected and produce different parameters.

 

From my understanding your code is similar to my first version in message 3 that will only select the total profile.

Use iLogic Copy? Please consider voting for this long overdue idea (not mine):https://forums.autodesk.com/t5/inventor-ideas/string-replace-for-ilogic-design-copy/idi-p/3821399
0 Likes
Message 15 of 17

Stakin
Collaborator
Collaborator

Sorry, I just read the landlord's question and didn't read the previous reply,

Your understanding is correct. If the same sketch contains different profiles, your method is better.

However, when your code is run here, a selection prompt will appear. After selection, an error will appear after pressing ESC

0 Likes
Message 16 of 17

gcoombridge
Advisor
Advisor

@Stakin no error when I run it in 2021. Either when I hit escape or select all profiles....

Use iLogic Copy? Please consider voting for this long overdue idea (not mine):https://forums.autodesk.com/t5/inventor-ideas/string-replace-for-ilogic-design-copy/idi-p/3821399
0 Likes
Message 17 of 17

RStancescu
Collaborator
Collaborator

I will have more feedback in a few days. It looks like will be several profiles in one sketch. The parameters that are created will be used in some formulas. (for thermal calculation) Would be nice if after selecting the profiles and creating the parameters, a label would be displayed in the sketch. 

I'm only afraid now that user could select the profiles in a different way after editing the sketch. 

Again, appreciate all your help and will be back at the end of week.

Have a great weekend!

Radu

0 Likes