Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

macro applying command to selection (several elements) 1

Status
Not open for further replies.

JeniaL

Mechanical
Jun 3, 2014
547
0
0
IL
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.
 
Replies continue below

Recommended for you

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.
 
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
 
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


 
[ul]
[li]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.[/li]
[li]Make sure you clean your codes up, there was a lot of unused variables that made it confusing[/li]
[li]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 :).[/li]
[li]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.[/li]
[li]As I mentioned before, using disassemble is not a good example for looping so I changed the code to throw a messagebox.[/li]
[/ul]
Code:
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"[b][COLOR=#4E9A06]'selection filter forces user to select specific objects, BiDim lets you pick a surface or face[/color][/b]
[b][COLOR=#4E9A06]'You don't want them to pick a face though so give some instructions in your message box[/color][/b]

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

Selection.Clear [b][COLOR=#4E9A06]'You should clear the selection before making a selection[/color][/b]

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

oStatus = Selection.SelectElement3(aSelectionFilter, sMessage, True,CATMultiSelTriggWhenUserValidatesSelection, False)
[b][COLOR=#4E9A06]'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[/color][/b]

If oSelection.Count = 0 then
[indent]msgbox "Nothing selected, Macro canceled"[/indent]
[indent]exit sub[/indent]
End if

[b][COLOR=#4E9A06]'Add selected objects to collection[/color][/b]
Redim aCollect(oSelection.Count - 1) [b][COLOR=#4E9A06]'You have to -1 because Arrays start at 0 but selections start at 1[/color][/b]

For i = 1 to oSelection.Count
[indent]aCollect(i-1) = oSelection.item(i).Value[/indent]
Next

oSelection.Clear

[b][COLOR=#4E9A06]'Do something to each collected object, This area may need some changes to work[/color][/b]
For i = 0 to uBound(aCollect)
[indent]oSelection.Add aCollect(i).value [b][COLOR=#4E9A06]'May need to take out .value...cant remember[/color][/b][/indent]
[indent]msgbox oSelection.Value.Name[/indent]
[indent]oSelection.clear[/indent]
Next

End Sub
 
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
download.aspx


Code:
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
 
Hi,

Workaround, in CATScript.

Code:
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

- Romania
- EU
 
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.
 
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].
 
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

- Romania
- EU
 
Status
Not open for further replies.
Back
Top