×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

macro applying command to selection (several elements)

macro applying command to selection (several elements)

macro applying command to selection (several elements)

(OP)
hi guys. trying to figure out how to apply a command to several selected elements. i can select several elements without any problem but i'm having issues how then to apply a command to my selection.
do you have any examples of how to do that?
any help will be appreciated.
cheers.

RE: macro applying command to selection (several elements)

(OP)
with a dialog box. for example.
message box please select contours for disassembling.
now i want to apply disassemble or whatever command to all selected contours.

RE: macro applying command to selection (several elements)

I believe with disassemble you need to use Catia.StartCommand "Disassemble"

The problem is this only launches the disassemble window but doesn't click the buttons for you...you need user interaction to click the option you want and the ok button...hence the macro will not wait for you to do that.

In theory you would want to loop through all selected objects and perform some task. You may need to add the selected items to collection because the operation you want to perform will not work with multiple objects in the selection.

'Add selected objects to collection
Dim cObjects as Collection
For i = 1 to oSelection.Count
cObjects.Add oSelection.item(i).value
Next

oSelection.Clear

'do something to all the objects
For i = 1 to cObjects.count
'add one of the objects to the selection
oSelection.Add cObjects.item(i)
'Do something
Catia.StartCommand "Disassemble"
oSelection.Clear
Next

Again, this is a bad example because the macro will not wait for you to pick the buttons on the Disassemble window...but hopefully you get the idea

RE: macro applying command to selection (several elements)

(OP)
thanks a lot for the idea. i was thinking about something similar.

RE: macro applying command to selection (several elements)

(OP)
i get the error at cObject. object required

CATVBS

Sub CATMain()

Dim Seleton
Dim cObject
Dim USelLB
Dim InputObject(0)
Dim oStatus
'Dim oSel As Selection
'Dim oSelItem As Object
Set Selection = CATIA.ActiveDocument.Selection
'Set cObject = CreateObject

InputObject(0) = "AnyObject"'selection filter forces user to select specific objects, AnyObject allows selection of any object

'Set Selection = CATIA.ActiveDocument.Selection
'Set USelLB = USel


Msgbox "PRESS OK AND SELECT ELEMENTS"

Selection.Clear'You should clear the selection before making a selection

oStatus = Selection.SelectElement3(InputObject, "Select objects to list names", True,CATMultiSelTriggWhenUserValidatesSelection, False)

'Add selected objects to collection
Dim cObjects
Set cObject = Selection
For i = 1 to Selection.Count
cObjects.Add Selection.item(i).value
Next

Selection.Clear

'do something to all the objects
For i = 1 to cObjects.count
'add one of the objects to the selection
Selection.Add cObjects.item(i)
'Do something
Catia.StartCommand "Disassemble"
Selection.Clear
Next

End Sub


RE: macro applying command to selection (several elements)

  • You Dim cObject 2 times, I would stick with oSel or oSelection as your selection variable...using cObjects is confusing because the c notation made me think it was a collection.
  • Make sure you clean your codes up, there was a lot of unused variables that made it confusing
  • Collections are only available in VBA, CATScript/catvbs you would need to use an array to collect objects. I think the code below will work :).
  • You may want to narrow your selection filter to make sure they are picking the type of object you want them to pick. With AnyObject they could pick a part...but you can't disassemble a part.
  • As I mentioned before, using disassemble is not a good example for looping so I changed the code to throw a messagebox.

CODE --> catvbs

Sub CATMain()

Dim oSelecton
Dim oSelectionLB
Dim oStatus

Dim aSelectionFilter(0)
Dim aCollect()

Dim sMessage 'as string

Set oSelection = CATIA.ActiveDocument.Selection
Set oSelectionLB = oSelection

aSelectionFilter(0) = "BiDim"'selection filter forces user to select specific objects, BiDim lets you pick a surface or face
'You don't want them to pick a face though so give some instructions in your message box

sMessage = "Select a surface feature from the specification tree"

Selection.Clear 'You should clear the selection before making a selection

Msgbox "1. Press OK" & vbCrLf & "2. " & sMessage

oStatus = Selection.SelectElement3(aSelectionFilter, sMessage, True,CATMultiSelTriggWhenUserValidatesSelection, False)
'You really dont need ostatus, it is for checking if the selection was Normal
'You can just use: Selection.SelectElement3 aSelectionFilter, sMessage, True,CATMultiSelTriggWhenUserValidatesSelection, False

If oSelection.Count = 0 then
msgbox "Nothing selected, Macro canceled"
exit sub
End if 'Add selected objects to collection Redim aCollect(oSelection.Count - 1) 'You have to -1 because Arrays start at 0 but selections start at 1 For i = 1 to oSelection.Count
aCollect(i-1) = oSelection.item(i).Value
Next oSelection.Clear 'Do something to each collected object, This area may need some changes to work For i = 0 to uBound(aCollect)
oSelection.Add aCollect(i).value 'May need to take out .value...cant remember
msgbox oSelection.Value.Name
oSelection.clear
Next End Sub

RE: macro applying command to selection (several elements)

(OP)
well with a following code a can select several curves and disassemble them. but i need each curve in a separate geo set. like curve 23 in geo set 1 and parallel in geo set 2


CODE --> vba

 
Sub CATMain()
Dim Document, Part, SELECTION, Status, sSel
Dim InputObjectType(0)

Set Document = CATIA.ActiveDocument
Set Part = Document.Part
Set SELECTION = Document.SELECTION

SELECTION.Clear

InputObjectType(0) = "AnyObject": iMsg = "Select curves..."

MsgBox "PRESS OK AND SELECT CURVES"

Status = SELECTION.SelectElement3(InputObjectType, iMsg, _
False, CATMultiSelTriggWhenUserValidatesSelection, True)

If ((Status = "Cancel") Or (Status = "Undo") Or (Status = "Redo")) Then Exit Sub

CrCount = SELECTION.Count

For i = 1 To CrCount
'Set SelectedElement = SELECTION.Item(i)
'Set myCurve = SelectedElement.Value

Dim hybridShapeFactory As Factory
Set hybridShapeFactory = Part.hybridShapeFactory

CATIA.StartCommand "Disassemble"
SELECTION.Clear
Next i

End Sub

RE: macro applying command to selection (several elements)

Hi,

Workaround, in CATScript.

CODE --> CATScript

Language="VBSCRIPT"
Sub CATMain()
MsgBox " Select curve (FROM TREE) to disassemble, it will be created in Disassembled Curve Geo Set. Only 50 loops, modify according to your needs or hit ESC to exit "

Set oDoc = CATIA.ActiveDocument
Set oPart = oDoc.Part
Set oHSF = oPart.HybridShapeFactory
Set oSel = oDoc.Selection

Dim Filt(0)
Filt(0) = "AnyObject" 
For i = 1 To 50

oSel.Clear
Ret = oSel.SelectElement2(Filt, "Select curve (FROM TREE) to disassemble", True)
	If (Ret = "Cancel") Then
	Exit Sub
	Else

Set oHB = oPart.HybridBodies.Add()
oHB.Name = "Disassembled Curve " & i

oSel.Search "Topology.CGMEdge,sel"
For n = 1 To oSel.Count
Set mySel = oSel.Item(n)

Set oCrvRef = mySel.Reference
miLongitud = Len(mySel.Reference.Name)
strTmp = Right(mySel.Reference.Name, miLongitud - 21)
miLongitud = Len(strTmp)
Texto= Left(strTmp, miLongitud - 1)
Set oRefCurva = oPart.CreateReferenceFromBRepName(Texto, mySel.Value)

Set oCurva = oHSF.AddNewCurveDatum(oRefCurva )
oCurva.Compute
oHB.AppendHybridShape (oCurva)

Next
End If
Next
End Sub 

Regards
Fernando

https://picasaweb.google.com/102257836106335725208 - Romania
https://picasaweb.google.com/103462806772634246699... - EU

RE: macro applying command to selection (several elements)

(OP)
thanks a lot Ferdo. any ideas how to do that with select element3? i want to select several curves and then run geo sets and disassembling creation.

RE: macro applying command to selection (several elements)

(OP)
yea you are right it's the same number of clicks. but it looks like interruption. select wait for creation select again and so on.
tried to edit the code a little bit. i can select several curves using select element3 but all curves after disassembling are located in the same geo set. cant figure out how to tell them to go different geo sets.

sorry for too much questions. guys i'm from Israel and we don't have any scripts training in here. the only way is to go abroad. 4 days course like 3 grands not including flight and a hotel pc.

RE: macro applying command to selection (several elements)

Awesome code Ferdo! You get a star.

RE: macro applying command to selection (several elements)

Hi,

Thank you lardman.

@JeniaL: There is also no scripting training in my country....I've learned all this stuff on hard way, reading, doing, trying, testing...lot of time lost reading again...and most important learning from advice coming from users like Eric, catiajim, littlechulu, gvi and few others...and I'm still learning from peoples which became more active on the forum in last period.

Funny thing is now I have to do the same thing in Enovia with v6...so you can imagine how I feel... nosmiley

Regards
Fernando

https://picasaweb.google.com/102257836106335725208 - Romania
https://picasaweb.google.com/103462806772634246699... - EU

RE: macro applying command to selection (several elements)

(OP)
oh yea i can imagine. switch back to v5

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources