I want to make a solid that automatically changes the volume

I want to make a solid that automatically changes the volume

ywjung
Explorer Explorer
178 Views
1 Reply
Message 1 of 2

I want to make a solid that automatically changes the volume

ywjung
Explorer
Explorer

Hello, Everyone

 

There are solid1 and solid2.
Changing the shape of Solid1 automatically changes the value of Solid2's 'Solid2Length' parameter and the volume of both solids is equal.
Only the current 'Solid2Length' parameter will be changed and the volume will be adjusted.
I want to create a rule so that the changing parameters are multiple parameters.
I hope you can help me.

 

Attach the ipt file that contains before.

 

'Header
Option Explicit On
 
 
Dim drivingParameterName = "Solid2Length"
 
' This rule tries to make the volume of Solid2 (the second solid in the part) equal to Solid1.
' It does in by changing the value a a single driving parameter.
' If you want other parameters to change as well, they must be driven by the driving parameter
' If you need iLogic rules to change the values of other parameters,
' make sure that the "Fire Dependent Rules Immediately" option is checked for this driving rule.
 
' Note: to make this rule work well, the volume of Solid2 must increase with every increade to the value 
' of the driving parameter. It must also decrease with every decrease.
 
' These are minimum and maximum values allowed for the driving parameter.
' They are used to constrain the search for a new value of the parameter.
Dim drivingMinValue As Double = 7 ' mm
Dim drivingMaxValue As Double = 80 ' mm
 
' Note: this rule is configured for parameter units of mm
' -----------------------------------------------------------
 
If drivingMaxValue <= drivingMinValue Then
Throw New ArgumentException("drivingMaxValue must be greater than drivingMinValue")
End If
 
Dim partDoc As PartDocument = ThisDoc.Document
 
Dim solid1 = partDoc.ComponentDefinition.SurfaceBodies(1)
Dim solid1Volume = solid1.Volume(0.01) ' allow 1 percent error
Dim solid2 As SurfaceBody
Dim solid2Volume As Double
 
Dim rangeMin = drivingMinValue * 0.1 ' convert to cm
Dim rangeMax = drivingMaxValue * 0.1 ' convert to cm
' Expand the range a bit to make it easier to find values that are exactly at min or max.
' (This also means that we can find values that are slightly outside of the min-max range.)
rangeMin -= 0.1
If rangeMin < 0.0 Then rangeMin = 0.0
rangeMax += 0.1
 
Dim currentMin = rangeMin
Dim currentMax = rangeMax
Dim currentValue As Double = Parameter.Param(drivingParameterName).Value
Dim newValue As Double
Dim iterationCount = 0
 
Dim done = False
Do
solid2 = partDoc.ComponentDefinition.SurfaceBodies(2)
solid2Volume = solid2.Volume(0.01)
done = EqualWithinTolerance(solid1Volume, solid2Volume, 0.1 * 0.001) ' tolerance in cubic mm, converted to cubic cm (* 0.001) 
If done Then Exit Do
If solid2Volume > solid1Volume Then
currentMax = currentValue
newValue = (currentValue + currentMin) * 0.5 ' average of current value and min
Else
currentMin = currentValue
newValue = (currentValue + currentMax) * 0.5 ' average of current value and max
End If
Parameter.Param(drivingParameterName).Value = newValue
partDoc.Update()
currentValue = newValue
iterationCount = iterationCount + 1
 
If iterationCount > 20 Then
Dim rangeSpan = rangeMax - rangeMin
If currentValue > rangeMax - 0.01 * rangeSpan Then
Throw New ArgumentException("Please increase drivingMaxValue in this rule.")
ElseIf currentValue < rangeMin + 0.01 * rangeSpan Then
Throw New ArgumentException("Please decrease drivingMinValue in this rule.")
End If
End If
Loop While Not done
 
Logger.Debug(" iterationCount = {0}", iterationCount)

 

0 Likes
179 Views
1 Reply
Reply (1)
Message 2 of 2

Curtis_Waguespack
Consultant
Consultant

Hi @ywjung 

 

I think what you had is working, with just a bit of an adjustment to the way it is updating. See the attached 2023 file.

 

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

 

 

EESignature

0 Likes