How to check if a named feature exists in a part file??

How to check if a named feature exists in a part file??

Anonymous
Not applicable
899 Views
2 Replies
Message 1 of 3

How to check if a named feature exists in a part file??

Anonymous
Not applicable

Hello All,

 

I have a rule (which is working prefectly) to calculate and report sweep length in a Part file (thanks to Rob van der Veek from FORUM Inventor Users Netherland), but, at first I would like to check if a specific named feature (Sweep1, e.g.) exists in a part file, and then, run that "length rule"...
If that feature don't exist, I do nothing...

 

The Sweep Length Rule is:

 

'DIM_SWEEP_R1
'
'Set a reference to the active part document
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveEditDocument

Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition


' Set a reference to the selected feature. Make sure you name the Sweep "Sweep1" or change this variable to match the Sweep you wish to calculate
Dim oSweep As SweepFeature
oSweep = oDef.Features.SweepFeatures.Item("Sweep1")

' Get the centroid of the sweep profile in sketch space
Dim oProfileOrigin As Point2d
oProfileOrigin = oSweep.Profile.RegionProperties.Centroid

' Transform the centroid from sketch space to model space
Dim oProfileOrigin3D As Point
oProfileOrigin3D = oSweep.Profile.Parent.SketchToModelSpace(oProfileOrigin)

' Get the set of curves that represent the true path of the sweep
Dim oCurves As ObjectsEnumerator
oCurves = oDef.Features.SweepFeatures.GetTruePath(oSweep.Path, oProfileOrigin3D)

Dim TotalLength As Double
TotalLength = 0

Dim oCurve As Object
For Each oCurve In oCurves

	Dim oCurveEval As CurveEvaluator
	oCurveEval = oCurve.Evaluator

	Dim MinParam As Double
	Dim MaxParam As Double
	Dim length As Double

	Call oCurveEval.GetParamExtents(MinParam, MaxParam)
	Call oCurveEval.GetLengthAtParam(MinParam, MaxParam, lengthp)

	TotalLength = TotalLength + lengthp
Next

Dim oparams As Parameters
Dim oparam As Parameter
oparams = oDoc.ComponentDefinition.Parameters
Dim exists As Boolean
exists = False

'Find out if parameter exists, if not it will create this parameter in the table, if you want another name then change Sweeplength to something else
For Each oparam In oparams
	If oparam.Name = "SWEEP_LENGTH" Then exists = True
Next oparam

'Change the value if the parameter exists otherwise add the parameter
If exists Then
	oparams.Item("SWEEP_LENGTH").Value = TotalLength
Else
	oparams.UserParameters.AddByValue("SWEEP_LENGTH", TotalLength, 11266)
End If
InventorVb.DocumentUpdate()

Thank you for the support,

 

Reinaldo Roth

 

0 Likes
Accepted solutions (1)
900 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Accepted solution

Hi, does this do what you wanted ?

 

SyntaxEditor Code Snippet

Sub Main()
	
 Dim oPartDoc As Inventor.PartDocument = ThisApplication.ActiveDocument
 
Dim oFeatures As PartFeatures
oFeatures = oPartDoc.ComponentDefinition.Features	
Dim oFeature As PartFeature


 
	For Each oFeature In oFeatures
	If oFeature.Name = "Extrusion1"  'Feature you are looking for
		iLogicVb.RunRule("RuleName")  'Name of your Rule
Else
End If 
Next 
		
		
	End Sub 
	
0 Likes
Message 3 of 3

Anonymous
Not applicable

Yess...

 

Sorry for the delay ... I was traveling ...

 

Thank you!!!

0 Likes