Constraint on Line Endpoint Macro
Constraint on Line Endpoint Macro
(OP)
Hello!
I need some help here. I am writing macro to constraint cilindrical part to line (in geometrical set of another part) with only user click being the one on this line of another part. Part1 has published axis and mating face. Orientation should be always the same (i solved this) and mating face should be 0mm (coincidence) with end point of line selected by user. Now... The problem is that my macro works if these selected line is not renamed, aka line name should be "Line.x", with x being automaticaly assigned number at creation of line.
Since i haven't found another way i used CreateReferenceFromName when creating reference from endpoint of line. Recorded string for reference is this :
"Product2/Part1.1/!Selection_BorderFVertex:(BEdge:(Brp:(GSMLine.1;2);None:(Limits1:();Limits2:();-1);Cf11:());GSMLine.1;InSameTool;Z0;G3055)"
In order to make it usable on any selected line bolded parts should be made as variables. "Product2/Part1.1" is easy. "-1" (can be "+1") depends on orientation of line and is easily accessible via VBA. Also nuber 2 after GSMLine.1 also depends on orinetation (can be 1 instead of 2), but this is easy.
Problem is with GSMLine.1. If you don't rename lines macro works (at least on my test model) but if you rename line it fails on CreateReferenceFromName. I recorded macro to see why is this happening on renamed lines and it looks that this GSMLine.1 remains the same even after line renaming and i cannot access this name at this point via selection.Item(i).Value.Name. Just to make clear... this line represents vertexes not the line.
Any advice is more than welcome, but please note that i would like to avoid creating additional elements or force user to select point himself. My full code is below.
Thank you in advance
Code:
I need some help here. I am writing macro to constraint cilindrical part to line (in geometrical set of another part) with only user click being the one on this line of another part. Part1 has published axis and mating face. Orientation should be always the same (i solved this) and mating face should be 0mm (coincidence) with end point of line selected by user. Now... The problem is that my macro works if these selected line is not renamed, aka line name should be "Line.x", with x being automaticaly assigned number at creation of line.
Since i haven't found another way i used CreateReferenceFromName when creating reference from endpoint of line. Recorded string for reference is this :
"Product2/Part1.1/!Selection_BorderFVertex:(BEdge:(Brp:(GSMLine.1;2);None:(Limits1:();Limits2:();-1);Cf11:());GSMLine.1;InSameTool;Z0;G3055)"
In order to make it usable on any selected line bolded parts should be made as variables. "Product2/Part1.1" is easy. "-1" (can be "+1") depends on orientation of line and is easily accessible via VBA. Also nuber 2 after GSMLine.1 also depends on orinetation (can be 1 instead of 2), but this is easy.
Problem is with GSMLine.1. If you don't rename lines macro works (at least on my test model) but if you rename line it fails on CreateReferenceFromName. I recorded macro to see why is this happening on renamed lines and it looks that this GSMLine.1 remains the same even after line renaming and i cannot access this name at this point via selection.Item(i).Value.Name. Just to make clear... this line represents vertexes not the line.
Any advice is more than welcome, but please note that i would like to avoid creating additional elements or force user to select point himself. My full code is below.
Thank you in advance
Code:
CODE --> VBA
Language="VBSCRIPT"
Sub CATMain()
Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument
Dim product1 As Product
Set product1 = productDocument1.Product
Dim selection As Selection
Set selection = productDocument1.selection
selection.Clear
ReDim sFilter(0)
MsgBox "Select Line."
sFilter(0) = "Line"
sStatus = selection.SelectElement2(sFilter, "Select Line.", False)
If (sStatus = "Cancel") Then
Exit Sub
End If
Dim inst As String
inst = selection.Item(1).LeafProduct.Name
MsgBox (selection.Item2(1).Reference.DisplayName)
'Dim geoset As String
'geoset = referenceChosen.Parent.Parent.Name
Dim ref2String As String
ref2String = product1.Name & "/" & inst & "/!" & selection.Item(1).Value.Name
Dim referenceChosen As Reference
Set referenceChosen = product1.CreateReferenceFromName (ref2String)
Dim constraints1 As Collection
Set constraints1 = product1.Connections("CATIAConstraints")
Dim reference1 As Reference
Dim Axis As Publication
Set Axis = product1.products.Item(product1.products.Count).Publications.Item("Axis")
Set reference1 = Axis.Valuation
Dim reference2 As Reference
Set reference2 = product1.CreateReferenceFromName(ref2String)
Dim constraint1 As Constraint
Set constraint1 = constraints1.AddBiEltCst(catCstTypeDistance, reference1, referenceChosen)
Dim length1 As Dimension
Set length1 = constraint1.Dimension
length1.Value = 0.000000
If selection.Item(1).Value.Orientation = 1 Then
constraint1.DistanceConfig = catCstDCParallelOppOrient
Else
constraint1.DistanceConfig = catCstDCParallelSameOrient
End If
Dim reference3 As Reference
Dim MeasureFace As Publication
Set MeasureFace = product1.products.Item(product1.products.Count).Publications.Item("Measure Face")
Set reference3 = MeasureFace.Valuation
Dim predznak As String
Dim nmb As String
If selection.Item(1).Value.Orientation = 1 Then
sign = "-"
nmb="2"
Else
sign="+"
nmb="1"
End If
Dim ref4String As String
ref4String = product1.Name & "/" & inst & "/!Selection_BorderFVertex:(BEdge:(Brp:(GSM" & selection.Item(1).Value.Name & ";" & nmb & ");None:(Limits1:();Limits2:();" & sign & "1);Cf11:());GSM" & selection.Item(1).Value.Name & ";InSameTool;Z0;G3173)"
MsgBox (ref4String)
Dim reference4 As Reference
Set reference4 = product1.CreateReferenceFromName(ref4String)
Dim constraint2 As Constraint
Set constraint2 = constraints1.AddBiEltCst(catCstTypeOn, reference3, reference4)
product1.Update
End Sub 




RE: Constraint on Line Endpoint Macro
Set reference2 = product1.CreateReferenceFromName(ref2String)
You should change this to:
Set reference2 = product1.CreateReferenceFromObject(selection.Item(1).Value.Parent)
Anyway, take a look into my thread ( http://www.eng-tips.com/viewthread.cfm?qid=343048 ) to see method for getting type of object (for validating rules) -
msgbox TypeName(selection.Item(1).Value.parent)
will return GenerativeShapeLine
LukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
RE: Constraint on Line Endpoint Macro
First of all, this reference2 is not reference in question. What causes me problems is Ref4String and reference4 in my code. However, your code looks more tidier so i tried that for Ref2 and it gives me error. I guess its because you cannot put CreateReferenceFromObject on product level if refereced object is Shape from part (code in your thread is making references on part, mine is on product). Also, this is first time i see that selecting object from tree is not the same thing as clicking on it in graphic window. I don't think it makes any difference.
I don't have now time to look your thread in detail but i ll check it later.