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!

Part Body - Define as in work object 1

Status
Not open for further replies.

Alan Lowbands

Aerospace
May 17, 2017
274
GB
Hi Gents,
I'm trying to get all the selected parts in an assembly to have the main body set as the 'in work object'.
Could anyone tell me where i'm going wrong please ?

thanks
Alan

------------------------------------
Language="VBSCRIPT"

Sub CATMain()

Set oSel = CATIA.ActiveDocument.Selection
oSel.Search "CATPrtSearch.BodyFeature,all"

intNbSelected = oSel.Count2
If intNbSelected > 0 Then
For intIndex = 1 to intNbSelected

Dim oPart
Set oPart = oSel

Set documents1 = oPart
Set partDocument1 = documents1

Set part1 = partDocument1
Set bodies1 = part1.Bodies
Set body1 = bodies1.Item("PartBody")
part1.InWorkObject = body1

Next
End If
oSel.Clear
End Sub
 
Replies continue below

Recommended for you

Hi Alan,
ANY part has ALWAYS a PartBody (which is called MainBody in VBA)
so, you need to loop thru your assembly, and for each part encountered do
part1.InWorkObject = part1.MainBody


regards,
LWolf
 
these lines make no sense...
Dim oPart
Set oPart = oSel 'since oSel is a Selection, and I am assuming by oPart you wanna have the part...

Set documents1 = oPart 'even here, you set documents1 to be, in fact, oSel...
Set partDocument1 = documents1 'and here...

regards,
LWolf
 
I took it that oSel would be the result of the search and so would be the part body.
When I run the code it does select all the part bodies in the assembly.
I'm struggling with selecting the parts / bodies and making them active.

cheers gents i'll have another go
 
the big part in your task is to loop thru all the parts...
usually that is achieved with a recursive loop.
as you can see, the sub ToTheJob is quite easy...

Option Explicit
Sub CATMain()

Dim ActiveModel
Set ActiveModel = CATIA.ActiveDocument
ActiveModel.Product.ApplyWorkMode DESIGN_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 Sub_Product(products1.item(k))
ElseIf Right(FullPath, 4) = "Part" Then
Call DoTheJob(products1.item(k))
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 Sub_Product(oProducts.item(k))
ElseIf Right(FullPath, 4) = "Part" Then
Call DoTheJob(oProducts.item(k))

End If
Next

End Sub

Sub DoTheJob(oProduct)
Dim myPart
Set myPart = oProduct.ReferenceProduct.Parent.Part
myPart.InWorkObject = myPart.MainBody
End Sub




regards,
LWolf
 
Thanks Lwolf :)
-------------------------------------------------------
If Right(FullPath, 7) = "Product" Then
Call Sub_Product(products1.item(k))
ElseIf Right(FullPath, 4) = "Part" Then
Call DoTheJob(products1.item(k))
------------------------------------------------------

Could I ask what the ", 7" & ", 4 does ?

cheers, that's brilliant
 
right is a function that takes a string, and looks up the characters from right...
pls. google for it. So basically the last 7 characters need to be "Product"
and so the other one checks that the last four characters spell out Part...
Alan, if you want to learn some, then by all means do search the internet... start your searches with "vbscript function.to.learn.about"


regards,
LWolf
 
Hi.

A few notes on code above:
1. "Option Explicit" is the absolute must as it eliminates errors that are VERY hard to track down. I wish it was applied by default, but Microsoft thought otherwise.
2. Better not to use file extension to validate object type. Code above requires extra tuning to work with case-sensitive file extensions (i.e. "CATPART", "catproduct")
There is a direct way to determine object type - use TypeName() function (or "is" VBA statement)
3. "Call" statement is redundant and what's worse makes code non-portable to CATScript (which is always better choice over VBA unless you need UI forms).
4. For..To is not required as well, it's slower and less convenient than For..Each

Code:
Sub Sub_Product(ActiveModel)
Dim prd, strType
For each prd in ActiveModel.Products
  strType = TypeName(prd.ReferenceProduct.Parent)
  If strType = "ProductDocument" then
    Sub_Product prd
  ElseIf strType = "PartDocument" then
    DoTheJob prd
  End If
Next
End Sub
 
Thanks guys,
Lwolf, I have googled loads regards scripting and bought the vb scripting course from Emmit Ross but I do struggle getting my head around it.
I will be spending a little more time on it next year.

While i'm here, could you help with the other thread I've posted.
Spent hours trying to get the value from the 'Length' properties box but keeps giving me the value from the 'WIDTH' box.
Is this because the designer used 'Length' instead of 'LENGTH' or some other term that isn't already a property ???

cheers guys and have a good Christmas and new year :)
 
Hi

Pitty is not like this in 3DEx...

Code:
Sub CATMain()

Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument

Dim selection1 As Selection
Set selection1 = productDocument1.Selection

selection1.Search "CATProductSearch.Part,all"

For X = 1 To selection1.Count2
    Dim part As part
    Set part = selection1.Item2(X).Value.ReferenceProduct.Parent.part
   
    part.InWorkObject = part.MainBody

Next

End Sub

Regards
Fernando

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top