Home > Back-end >  How do I add a product to a selected item in CATScript/VBScript?
How do I add a product to a selected item in CATScript/VBScript?

Time:10-11

I have been puzzling with CATscript for CATIA v5 and I am trying to do the following:

Before I would like to run the Macro, I would like to Select a Product out of a tree with 100's of products.

Then run a Macro that does the following, irrelevant of the name, level, of the selected product so the script can be used in different assemblies and it should not matter if it is the root or the child of a child of a child:

-Ask for a Name
-Create a new Product as a child of the selected product with A_name
-Create 4 products as children of the new A_name product called B_name, C_name, etc.
-End

I have managed to add it to the root of the current document, but I cannot get it to add the product to the selected item.

When I try to replace the part that concerns the root with all kinds of definitions of

Dim oSel As Product
Set oSel = CATIA.Active.Document.Selection

I seem to not be able to define it as a collection and thus the script does not want to run anymore. Current code for adding the structure to the root product:

Language="VBSCRIPT"
Sub CATMain()

'Select the root document with the Product type
Dim oProductDoc As Document
Set oProductDoc = CATIA.ActiveDocument

'Retrieve the root product.
Dim oRootProduct As Product
Set oRootProduct = oProductDoc.Product

'Retrieve the product's collection under the root product.
Dim oRootChildren As Products
Set oRootChildren = oRootProduct.Products

'Ask for a name.  
sName=InputBox("Please enter a name for the assembly", "input", "name")

'Add a new product to the collection.
Dim oMainChildProduct As Product
Set oMainChildProduct = oRootChildren.AddNewComponent("Product", "")

'Declare the part number and name for this product.
oMainChildProduct.PartNumber = "A_" & sName
oMainChildProduct.Name = "A_" & sName

'Retrieve the MainChildProduct's collection
Dim oMainChildProducts As Products
Set oMainChildProducts = oMainChildProduct.Products

'Add a new product to the collection.
Dim oChildProduct1 As Product
Set oChildProduct1 = oMainChildProducts.AddNewComponent("Product", "")

'Declare the part number and name for this product.
oChildProduct1.PartNumber = "B_" & sName
oChildProduct1.Name = "B_" & sName

'Add a new product to the collection.
Dim oChildProduct2 As Product
Set oChildProduct2 = oMainChildProducts.AddNewComponent("Product", "")

'Declare the part number and name for this product.
oChildProduct2.PartNumber = "C_" & sName
oChildProduct2.Name = "C_" & sName

'Add a new product to the collection.
Dim oChildProduct3 As Product
Set oChildProduct3 = oMainChildProducts.AddNewComponent("Product", "")

'Declare the part number and name for this product.
oChildProduct3.PartNumber = "D_" & sName
oChildProduct3.Name = "D_" & sName

'Add a new product to the collection.
Dim oChildProduct4 As Product
Set oChildProduct4 = oMainChildProducts.AddNewComponent("Product", "")

'Declare the part number and name for this product.
oChildProduct4.PartNumber = "E_" & sName
oChildProduct4.Name = "E_" & sName

End Sub

Thank you so much for any help you can offer!

P.S. The 'Superdeluxe' version of the script should turn the A_Name Product into an Geometrical Bundle (so not a set) and add the electrical publications required to add Multibrancheables to the A_name Bundle, but since it took me multiple emails to the Dassault helpdesk themselves to explain the difference between a geo. set and a geo. bundle, it made me gave up hope on this one. When I try to record the conversion to a makro, the code only shows a rename and not an actual conversion.

CodePudding user response:

Here an example how to select a product in an assembly.

Sub CATMain()

dim oSel as Selection
dim InputObjectType() as String
dim Status as String
dim oProductToInsertChilds as Product
dim oChildProduct as Product

Set oSel = CATIA.ActiveDocument.Selection

ReDim InputObjectType(0)
InputObjectType(0)="Product" 

Status = oSel.SelectElement2(InputObjectType,"Select a Product",false)

if Status <> "Normal" then
    'User canceled the selection
    Exit Sub
end if

Set oProductToInsertChilds = oSel.Item2(1).Value

if TypeName(oProductToInsertChilds.ReferenceProduct.Parent) <> "ProductDocument" then
    'an instance of a PartDocument was selected
    Exit Sub
end if

'now do something with the oProductToInsertChilds
Set oChildProduct = oProductToInsertChilds.Products.AddNewComponent("Product", "")

End Sub
  • Related