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_BorderFVertexBEdgeBrpGSMLine.1;2);NoneLimits1);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_BorderFVertexBEdgeBrpGSMLine.1;2);NoneLimits1);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:
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