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!

Modify Part Script to Work At Assembly

Status
Not open for further replies.

jzecha

Aerospace
Jan 20, 2016
235
US
Can somebody please help me modify this code which adds 3 new Parameters to a part to work at the assembly level and adds these parameters to every part and product?

Code:
Language="VBSCRIPT"

Sub CATMain()

Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument

Dim product1 As CATBaseDispatch
Set product1 = partDocument1.GetItem("")


Dim parameters1 As Parameters
Set parameters1 = product1.UserRefProperties

Dim strParam1 As StrParam
Set strParam1 = parameters1.CreateString("DETAIL NUMBER", "")

strParam1.ValuateFromString ""

Dim parameters2 As Parameters
Set parameters2 = product1.UserRefProperties

Dim strParam2 As StrParam
Set strParam2 = parameters2.CreateString("MATERIAL", "")

strParam2.ValuateFromString ""

Dim parameters3 As Parameters
Set parameters3 = product1.UserRefProperties

Dim strParam3 As StrParam
Set strParam3 = parameters3.CreateString("STOCK SIZE", "")

strParam3.ValuateFromString ""

End Sub
 
Replies continue below

Recommended for you

Hello,
you just need too use multiple selection, for those parts from assembly and then run for loop to set these parameters.
You need to define "partDocument1" using selection.

 
Instead of using a partdocument object, use the activedocument object. Activedocument can contain parts, drawings, or assemblies. Not all functions behave the same on all types of objects though.

You are also will want to run through a loop to assign the parameters to each subassy and subproduct while walking down your assembly structure step by step. You will need to use the appropriate "collectors" to do that.

-or-

Search for the objects using a criteria and then assign your parameters to each object in the items that were selected by the search. Search syntax is really powerful.

Here is an example of hiding every geometric set in every part in an assembly of arbitrarily large size. Three lines of code. The heavy lifting happens on the first line. You could probably modify this to add your parameters to every part and assembly. Use the GUI search function to figure out your search syntax.

CATIA.ActiveDocument.Selection.Search "'Generative Shape Design'.'Geometrical Set'.Visibility=Shown, all"
CATIA.ActiveDocument.Selection.VisProperties.SetShow catVisPropertyNoShowAttr
CATIA.ActiveDocument.Selection.Clear

BTW, I am a hack macro programmer. Don't listen to me if someone better comes along.
 
IMHO selection performs badly on large models.
I've utilized recursion...


Sub CATMain()

Dim ActiveModel
Set ActiveModel = CATIA.ActiveDocument
ActiveModel.Product.ApplyWorkMode DEFAULT_MODE
Dim products1 'As Products
Set products1 = ActiveModel.Product.Products
Dim k As Integer
Dim FullPath As String

For k = 1 To products1.Count

FullPath = products1.item(k).ReferenceProduct.Parent.FullName

If Right(FullPath, 7) = "Product" Then
Call DoTheJob(products1.item(k).ReferenceProduct)
Call Sub_Product(products1.item(k))​
ElseIf Right(FullPath, 4) = "Part" Then
Call DoTheJob(products1.item(k).ReferenceProduct)​
End If​
Next

End Sub

Sub Sub_Product(ActiveModel)

Dim oProducts
Set oProducts = ActiveModel.Products
Dim oProduct
Dim FullPath As String
Dim k As Integer
For k = 1 To oProducts.Count

FullPath = oProducts.item(k).ReferenceProduct.Parent.FullName
If Right(FullPath, 7) = "Product" Then

Call DoTheJob(oProducts.item(k).ReferenceProduct)
Call Sub_Product(oProducts.item(k))​

ElseIf Right(FullPath, 4) = "Part" Then
Call DoTheJob(oProducts.item(k).ReferenceProduct)​
End If​
Next

End Sub

Sub DoTheJob(oProductRefProduct)
Dim UserRefParams As Parameters
Set UserRefParams = oProductRefProduct.UserRefProperties

Dim DetailNum As StrParam
Set DetailNum = UserRefParams.CreateString("DETAIL NUMBER", "")
DetailNum.ValuateFromString ""

Dim Mtrl As StrParam
Set Mtrl = UserRefParams.CreateString("MATERIAL", "")
Mtrl.ValuateFromString ""

Dim StockSize As StrParam
Set StockSize = UserRefParams.CreateString("STOCK SIZE", "")
StockSize.ValuateFromString ""
End Sub


regards,
LWolf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top