Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constraint on Line Endpoint Macro

Status
Not open for further replies.

kicosh

Automotive
Mar 22, 2012
10
HR
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:
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
 
Replies continue below

Recommended for you

Why wouldn't You simply select line (geometrical object - same as selecting line from part tree) instead of selecting it from graphic window? When You select it form graphic window, selected object is not reffering to line (whole line), but it's linear part (1-D), in this same way You can select vertex of line (start and endpoints) from graphic window, but not from tree.


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 ( ) 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
 
Thank you for your answer but this does not solve my problem.

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.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top