0

I would like to get the Part Number from Selection. I guess because I get the info from Enovia5 so I encounter an issue: some I can get the Part Number and some return Catastrophic failure. I ensure the selection always has 1 item only.

   CATIA.ActiveDocument.Selection.Item(1).LeafProduct.PartNumber '<~~~ sometimes works, sometimes does not. 

Note: I use TypeName to check what type of file gets selected, all return Document but the ones return error actually have .CATPart when I view their properties. Error mismatch will return if I try to declare it as Part or PartDocument.

Dim myPart as Part
Set myPart = CATIA.ActiveDocument.Selection.Item(1).value '<~~~ Mismatch
Set myPart = CATIA.ActiveDocument.Selection.Item(1).LeafProduct'<~~~ Mismatch

Dim myPartDoc as PartDocument
Set myPart = CATIA.ActiveDocument.Selection.Item(1).value '<~~~ Mismatch
Set myPart = CATIA.ActiveDocument.Selection.Item(1).LeafProduct'<~~~ Mismatch
N.TW12
  • 241
  • 2
  • 11
  • 1
    What have you selected? _LeafProduct_ should always return a product. – Shrotter Mar 07 '23 at 06:56
  • @Shrotter, I tried both: ‘.value’ and ‘.LeftProduct’ and same results, same errors. It could be that my employer has some weird settings on their Enovia5 – N.TW12 Mar 07 '23 at 19:28
  • 1
    Some products (or one some conditions) the partnumber is not accessible (e.g. cache-mode). What have you selected? What is special in the cases where you get an error? (I'm not familiar with Enovia) – Shrotter Mar 07 '23 at 20:14
  • @Shrotter, the error occurs when I select the ones with .CATPart, no issue with the actual product files. I cannot really do anything because CATIA still consider these .CATPart as product so I cannot Set it to Part and process. – N.TW12 Mar 07 '23 at 23:19
  • 1
    _PartNumber_ is a property of a product. I guess _Leafproduct_ is on valid in a _ProductDocument_. What is your _ActiveDocument_? What have you selected? – Shrotter Mar 08 '23 at 05:11
  • ActiveDocument is a root of a Product, it is the top most from Enovia PRC (It is product Root Class). I can right-click on get the Part Number for everything in the tree but cannot achieve this via code. I will do some digging to see anything or a way to get around this. – N.TW12 Mar 08 '23 at 05:32
  • @Shrotter, I would like to add that no matter what I select, the CATIA.ActiveDocument is always the root. – N.TW12 Mar 08 '23 at 05:46
  • @Shrotter, if I go to "Design Mode" then no issue with `Dim myProduct as Product: set myProduct = CATIA.ActiveDocument.Selection.Item(1).Value` then get the `partnumber = myProduct.PartNumber`. The issue with this approach is it will take lot of time to load but if there is nothing I can do then probably I will go with activate the `Design Mode` – N.TW12 Mar 08 '23 at 06:26
  • @Shrotter, last update: it appears that I can automate applying between modes on the Selection. I just catch the error and then apply the `Design Mode`, get the `PartNumber`, then reverse it back. Thanks for the help, without you provided suggestion, I could not fingure this out. – N.TW12 Mar 08 '23 at 06:34
  • dim myPart as Product, no ? – Disvoys Mar 09 '23 at 20:51

1 Answers1

2

LeafProduct does not return Part nor PartDocument. It returns Product object. In your example, you are filling variables with objects of wrong type.

Product object is something like container around Part or Product(Product like assembly in catia)

There can be several problems. At first, I'd try to work with selection object separately.

Dim oDoc As Document
Set oDoc = CATIA.ActiveDocument

Dim oSel as Selection
Set oSel = oDoc.Selection

If oSel.Count = 1 Then
    Dim oProd as Product
    Set oProd = oSel.Item2(1).LeafProduct

    MsgBox oProd.PartNumber
Else
    MsgBox "More objects selected!!!"
End If

Then if you want to get Part object from this product and you are sure it is product which is container around part and not around Product(Assembly), you can get it easily like that:

Dim oPartDocument As PartDocument
Set oPartDocument = oProd.ReferenceProduct.Parent

Dim oPart As Part
Set oPart = oPartDocument.Part    

In your catia installation is file which can helps you to understand how to use API for automation. It is documentation for automation. You can find it here:

enter image description here

JMan
  • 60
  • 7