Access a sketch dimension by name

Access a sketch dimension by name

chrisw01a
Collaborator Collaborator
640 Views
9 Replies
Message 1 of 10

Access a sketch dimension by name

chrisw01a
Collaborator
Collaborator

I have found this code that works but how do I do this without the FOR statement? Can't I just call the dimension by its name somehow? I cannot get it to work.

 

Here is what I have that works:

'get the sketch to work with
oSketch = ThisDoc.Document.ComponentDefinition.Sketches.Item("Top of door frame s")
i=0
For
i = 1 To oSketch.DimensionConstraints.Count If oSketch.DimensionConstraints.Item(i).Parameter.Name = "doorAngle" Then oSketch.DimensionConstraints.Item(i).Driven = False Exit For End If Next 

 

How can I just say:

oSketch.DimensionConstraints.Item(doorAngle).Driven = False

 

It throws an error.

 

Thanks group.

 

0 Likes
Accepted solutions (1)
641 Views
9 Replies
Replies (9)
Message 2 of 10

dgreatice
Collaborator
Collaborator

you code is ok, but I don't know how complex you sketch. maybe you get over constraints/ over dimensions.

 

did you try manually to remove driven dimension, and what happen?

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 3 of 10

chrisw01a
Collaborator
Collaborator
What do you mean by "ok"?

This: (oSketch.DimensionConstraints.Item(doorAngle).Driven = False) Does not work.

No problems with over-contraining, it throws a code in the iLogic because the function wants an index number instead of a "name".

I think you can run the loop once and assign the constraint names to objects but I don't know how to set it up.

Thank you.
0 Likes
Message 4 of 10

Michael.Navara
Advisor
Advisor

Access dimension by its name is not possible, because DimensionConstraint has no name (except RefKey, but this is another job).

If you want to get DimensionConstraint by its parameter name on several places in your code, you can wrap this to separate function like

Function DimensionConstraintByName(sketch As Sketch, name As string) As DimensionConstraint
    For Each dimension As DimensionConstraint In sketch.DimensionConstraints
        If dimension.Parameter.Name = name Then Return dimension
    Next
    Return Nothing
End Function
Message 5 of 10

chrisw01a
Collaborator
Collaborator
Michael, this looks like what I was asking for. Can you please provide a short snippet showing how to implement it and convert a sketch dimension to a driven dimension?

Thank you.
0 Likes
Message 6 of 10

WCrihfield
Mentor
Mentor

Hi @chrisw01a.  When dealing with the 'Driven' vs 'Driving' aspect of sketch dimensional constraints, you have to do the process in the correct order or it will throw an error.  When turning a dimension's Driven property to False, that means you are attempting to make it the 'Driving' dimension.  Many times, if there is more than one dimension to the same geometry, and the 'other' dimension is currently the 'Driving' one, then you must first turn that 'other' dimension's 'Driven' property to True first, before you can turn 'this' dimension's 'Driven' property to False, otherwise it will throw an error, because you can't have two dimensions 'Driving' the same piece of geometry, and it is not smart enough to turn the 'other' dimension into a 'Driven' dimension for you when you do that.  Just something to keep in mind, to avoid further headaches in this process.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 10

chrisw01a
Collaborator
Collaborator

This is in fact why I was trying to simplify my code as much as possible.  Thank you.

 

iLogicVb.UpdateWhenDone = True
Parameter.UpdateAfterChange = True

Dim i As Long

'get the sketch to work with
oSketch = ThisDoc.Document.ComponentDefinition.Sketches.Item("Top of door frame s")

'If centered
If doorside = 2 Then
	For i = 1 To oSketch.DimensionConstraints.Count 
		If oSketch.DimensionConstraints.Item(i).Parameter.Name = "doorToFbRh" Then
			oSketch.DimensionConstraints.Item(i).Driven = True
		End If	
	Next
	For i = 1 To oSketch.DimensionConstraints.Count 
		If oSketch.DimensionConstraints.Item(i).Parameter.Name = "doorAngle" Then
			oSketch.DimensionConstraints.Item(i).Driven = False
		End If	
	Next
	Parameter("doorAngle") = 180
Else
	'If full Round
	If fbfr = 0 Then
		For i = 1 To oSketch.DimensionConstraints.Count 
		If oSketch.DimensionConstraints.Item(i).Parameter.Name = "doorToFbRh" Then
			oSketch.DimensionConstraints.Item(i).Driven = True
		End If	
		Next
		For i = 1 To oSketch.DimensionConstraints.Count 
			If oSketch.DimensionConstraints.Item(i).Parameter.Name = "doorAngle" Then
				oSketch.DimensionConstraints.Item(i).Driven = False
			End If	
		Next
		Parameter("doorAngle") = 90
	'If flat back & door RH
	ElseIf doorside = 0 Then
		'Set approximate distance to FB
		For i = 1 To oSketch.DimensionConstraints.Count 
			If oSketch.DimensionConstraints.Item(i).Parameter.Name = "doorAngle" Then
				oSketch.DimensionConstraints.Item(i).Driven = True
			End If	
		Next
		For i = 1 To oSketch.DimensionConstraints.Count 
			If oSketch.DimensionConstraints.Item(i).Parameter.Name = "doorToFbRh" Then
				oSketch.DimensionConstraints.Item(i).Driven = False
			End If	
		Next
		Parameter("doorToFbRh") = doorToFbMin
		
	'If flat back & door LH
	Else
		'do it manually because this doesn't work
	End If
End If
0 Likes
Message 8 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Well, we can shorten your main routine quite a bit by using the nice function provided by @Michael.Navara above, but I am not sure about your design intent, so I would not want to suggest how to lay the rest of your code out differently.  Here is my version of your code, implementing that function.

Sub Main
	iLogicVb.UpdateWhenDone = True
	Parameter.UpdateAfterChange = True
	Dim i As Long
	'get the sketch to work with
	oSketch = ThisDoc.Document.ComponentDefinition.Sketches.Item("Top of door frame s")
	Dim oDim1 As DimensionConstraint = DimensionConstraintByName(oSketch, "doorToFbRh")
	If IsNothing(oDim1) Then Exit Sub
	Dim oDim2 As DimensionConstraint = DimensionConstraintByName(oSketch, "doorAngle")
	If IsNothing(oDim2) Then Exit Sub
	'If centered
	If doorside = 2 Then
		oDim1.Driven = True
		oDim2.Driven = False
		Parameter("doorAngle") = 180
	Else
		'If full Round
		If fbfr = 0 Then
			oDim1.Driven = True
			oDim2.Driven = False
			Parameter("doorAngle") = 90
		'If flat back & door RH
		ElseIf doorside = 0 Then
			'Set approximate distance to FB
			oDim2.Driven = True
			oDim1.Driven = False
			Parameter("doorToFbRh") = doorToFbMin
		'If flat back & door LH
		Else
			'do it manually because this doesn't work
		End If
	End If
End Sub

Function DimensionConstraintByName(sketch As Sketch, name As String) As DimensionConstraint
    For Each dimension As DimensionConstraint In sketch.DimensionConstraints
        If dimension.Parameter.Name = name Then Return dimension
    Next
    Return Nothing
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 10

chrisw01a
Collaborator
Collaborator

Yes! That is what I was after. I'll give it a try.

 

Thank you for taking the time. That was really nice.

 

Chris

0 Likes
Message 10 of 10

chrisw01a
Collaborator
Collaborator
It works perfectly!

Once again, thank you for your time and effort.
0 Likes