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!

Selecting a CATPart or Sub-product to work in that document

Status
Not open for further replies.

l3ob

Aerospace
Apr 28, 2004
56
I have a VBscript that works well in a catpart but it fails in a catproduct. I want to be able to select the catpart or a sub-product within the top-product to make it active and/or set the partdocument from the list of documents within the product. In the code below, assuming I don't know the name of document "PartName.CATPart", I need to have code to select the tree item and Set partDoc1 from that selection.

Sub CATMain()

Set doc1 = CATIA.Documents

'--------------
Set partDoc1 = doc1.Item("PartName.CATPart")
>>> name not known, so I need to replace this line
>>> with selection process below
'--------------

InputObjectType(0)= "AnyObject"

Set oSelA = doc1.Selection

oSelA.clear

Result = oSelA.SelectElement(InputObjectType, "Select Tree Item", False)

If Result = "Cancel" Then
MsgBox ("Command Canceled")
Exit Sub
End If

Set partDoc1 = oSelA.Item(1)
>>>> this is where I'm stuck. I can't get the document
>>>> out of the selection to set partDoc1

Set part1 = partDoc1.Part

Set hybridShapeFactory1 = part1.HybridShapeFactory

 
Replies continue below

Recommended for you

Hi, You need to use the .Value property of the selected object, e.g. Set partDoc1 = oSelA.item(1).Value.

You should also use .SelectElement2 rather than SelectElement if you want the code to run in R11 onwards.

 
Tried that. The '.Value' gives a reference not a part document. The next line fails. I need to extract the part document from the selected tree item ( part or sub product )
to make active in whick to work. The 'Set partDoc1 = oSelA.item(1).Value.name' shows the name of the instance....'part1.1', its document would be 'part1.CATPart'.

 
See if this can give you what you are after...

Set doc1 = CATIA.ActiveDocument
InputObjectType = Array("Part", "Product")
Set osela = doc1.Selection
osela.Clear
Result = osela.SelectElement2(InputObjectType, "Select Part", False)
If Result = "Cancel" Then
MsgBox ("Command Canceled")
Exit Sub
End If
If osela.Item(1).Type = "Product" Then
' do some sub product code
Debug.Print "Selected Product"
ElseIf osela.Item(1).Type = "Part" Then
' do some part code
Debug.Print "Selected Part"
End If


If it is a product you need to use the PartNumber not the Name and you should be able to construct the appropriate document name from that, e.g.

set oDoc = CATIA.Documents.Item(selecteditem.PartNumber & ".CATProduct")

or something like that. However, you need to ensure that people don't select the 'Product node' for a part in the assembly tree (the node above the part in the tree). If they select the wrong node then the code assumes it is a product and not a part and you would get an error. You just need to put in some simple error trapping or limit them to just parts if this is possible.


 
From your suggestion, I ended up with the following code example that lets me select the Part in the tree and aquires the .CATPart document.

Within a CATProduct, it works well up until the 'COPY' line. It errors with : Selected element not allowed for this operation!

It works all the way through within just a CATPart.

Sorry to pick your brain, but, got any ideas?

Code:
Language="VBSCRIPT"

Sub CATMain()

Dim InputObjectType(0)
Dim InputDocType(0)
InputDocType(0) = "Part"

Set ActiveDoc = CATIA.ActiveDocument

' Prompt to select destination part document if a Product is active

Set oActDocSel = ActiveDoc.Selection
InputDocType(0) = "Part"
Result = oActDocSel.SelectElement2(InputDocType, "Select CATPart", False)
msgbox result

If Result = "Cancel" Then
MsgBox ("Command Canceled")
Exit Sub
End If

Set DestinationDoc = CATIA.Documents.Item(oActDocSel.Item(1).value.name & ".CATPart")

oActDocSel.Clear

Set oSelB = DestinationDoc.Selection

InputObjectType(0)= "Face"
Result = oSelB.SelectElement2(InputObjectType, "Select a face", False)

If Result = "Cancel" Then
MsgBox ("Command Canceled")
Exit Sub
End If

oSelB.copy <---- Error:Selected element not allowed for this operation!

msgbox "copied"

End Sub

 
Hi,

Try changing your script to:


Dim InputObjectType(0)
Dim InputDocType(0)
InputDocType(0) = "Part"

Set ActiveDoc = CATIA.ActiveDocument

' Prompt to select destination part document if a Product is active

Set oActDocSel = ActiveDoc.Selection
InputDocType(0) = "Part"
Result = oActDocSel.SelectElement2(InputDocType, "Select CATPart", False)
MsgBox Result

If Result = "Cancel" Then
MsgBox ("Command Canceled")
Exit Sub
End If

Set DestinationDoc = CATIA.Documents.Item(oActDocSel.Item(1).Value.Name & ".CATPart")

oActDocSel.Clear

InputObjectType(0) = "Face"
Result = oActDocSel.SelectElement2(InputObjectType, "Select a face", True)

If Result = "Cancel" Then
MsgBox ("Command Canceled")
Exit Sub
End If

oActDocSel.Copy '<---- Error:Selected element not allowed for this operation!

MsgBox "copied"


You should perform the copying using the top level document selection object (in this case the active document).
 
Thanks 'AHay'. This put me in the right direction. The program works in a catproduct and a catpart now.

I had to select the face first from a foreign CATPart within the Product, copy to clipboard , aquire the destination CATPart by selection, and Paste the Externally Linked Surface in the destination.

The only problem I have now is I want the program to check the selected Face properties to see if it is already in the selected destination CATPart and not foreign before I choose to copy/paste. In the full (larger) program, the face is used as a reference for creating normal lines through points.

Resulting Sample code that works:

Language="VBSCRIPT"
Sub CATMain()

Dim InputObjectType(0)
Dim InputDocType(0)

InputDocType(0) = "Part"
Set ActiveDoc = CATIA.ActiveDocument
Set ActDocSel = ActiveDoc.Selection

InputObjectType(0)= "Face"
Result = ActDocSel.SelectElement2(InputObjectType, "Select a Face ", False)

If Result = "Cancel" Then
MsgBox ("Command Canceled")
Exit Sub
End If

ActDocSel.copy
ActDocSel.Clear

'Select Destination CATPart

InputDocType(0) = "Part"
Result = ActDocSel.SelectElement2(InputDocType, "Select CATPart", False)

If Result = "Cancel" Then
MsgBox ("Command Canceled")
Exit Sub
End If

Set WorkDoc = CATIA.Documents.Item(ActDocSel.Item(1).value.name & ".CATPart")

WorkDoc.part.InWorkObject = WorkDoc.part
ActDocSel.PasteSpecial "CATPrtResult"
WorkDoc.part.update

Set Ref1 = ActDocSel.item(1).value

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor