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!

How do you get the Document's Instance Number? 1

Status
Not open for further replies.

Ruky

Aerospace
Sep 19, 2011
26
CA
Hi,

I have the following code below. My intent is to get all the instance partnumbers from all the documents in my activedocument.

I expect that the variable "nam" below will give me XXXXXXX.1 ,XXXXXXX.2, ...,XXXXXX.n
but the variable just returns XXXXXXXX.CATPART, XXXXXXXX.CATPART,....,XXXXXXXX.CATPART with no instance numbering.

I think nam = docus.Item(k).Name is the wrong approach but I do not know any other approach.

Any suggestions/solutions?
'------------
Sub CATMain()
Dim docus As Documents: Set docus = CATIA.Documents
Dim r As Integer

Dim i As Integer: i = docus.Count
For k = 1 To i
Dim nam As String: nam = docus.Item(k).Name
MsgBox nam
Next

End Sub
 
Replies continue below

Recommended for you

Hi ferdo,
That approach works, but if I want to use catvba all the way, is there another way of getting the instance numbers?

Thanks for the quick response
 
I've posted similiar code few days ago:

This code is listing all partnumbers and partnames according to their's positions in document's tree

Code:
Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If
   Call ListingNames(productDocument1.Product)

End Sub

Sub ListingNames(current_prod)
If current_prod.Products.Count > 0 Then
For i = 1 To current_prod.Products.Count
Call ListingNames(current_prod.Products.Item(i))
Next 
Else
MsgBox current_prod.Name
End If
End Sub


If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum
 
Code:
Sub CATMain()

REM Works in an active CATProduct

Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument
Dim selection1 As Selection
Set selection1 = productDocument1.Selection

Dim HSOSynchronizedFilter(0)
Dim sel
Dim Status
Set sel = CATIA.ActiveDocument.Selection
HSOSynchronizedFilter(0) = "SetCATIADotHSOSynchronizedToFalse"

selection1.Search "(CATAsmSearch.Part + CATAsmSearch.Product),all"

For i = 1 to selection1.Count 
SelectionList = SelectionList & chr(10) & selection1.Item(i).Value.Name 
Next

MsgBox "Selection is " & SelectionList 

selection1.Clear
HSOSynchronizedFilter(0) = "SetCATIADotHSOSynchronizedToTrue"

End Sub

Regards
Fernando

 
Hi ferdo,

That works! Although I've gotten a bit greedy and tried if I can acquire the parent product of each selected item the macro loops through. I tried doing this:

Prnt = selection1.item(i).value.parent.name. It worked well for the first item on the selection. But the next iteration gave "products" for Prnt. Does that mean that the loop is also looking at the grandparents of the item (i)?

If this is the case, how do I tell the compiler to just look at the immediate parent of item (i)?


Thanks!
 
Code:
Dim Listed
Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If
ProductName=""
   Call ListingNames(productDocument1.Product,ProductName)

Msgbox Listed
End Sub

Sub ListingNames(current_prod,pathname)
If current_prod.Products.Count > 0 Then
pathname2 = pathname &"\"& current_prod.name
For i = 1 To current_prod.Products.Count
Call ListingNames(current_prod.Products.Item(i),pathname2)
Next 
Else
'MsgBox pathname &"\" & current_prod.Name
Listed = Listed & chr(10) & pathname &"\" & current_prod.Name
End If
End Sub

This one would works at every level of assembly (I Reported my post below, there is small mistake, that I've noticed after submitting)

LukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
 
Yes! it works!!!....using .Parent twice is something very foreign to me. I didn't know that was possible.

Thanks!
 
I wrote small function:
Code:
Function AssemblyLevel(oProduct)
If oProduct.Parent.Parent.Name="CNEXT" Then
AssemblyLevel=1
Else
AssemblyLevel=AssemblyLevel(oProduct.Parent.Parent)+1
End If
End Function

and testing macro:
Code:
Dim Listed
Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If
ProductName=""
   Call ListingNames(productDocument1.Product,ProductName)

Msgbox Listed
End Sub

Sub ListingNames(current_prod,pathname)
If current_prod.Products.Count > 0 Then
pathname2 = pathname &"\"& current_prod.name
For i = 1 To current_prod.Products.Count
Call ListingNames(current_prod.Products.Item(i),pathname2)
Next 
Else
'MsgBox pathname &"\" & current_prod.Name
Listed = Listed & chr(10)&"Assembly level:"&AssemblyLevel(current_prod) &" path: " &pathname &"\" & current_prod.Name
End If
End Sub 

Function AssemblyLevel(oProduct)
If oProduct.Parent.Parent.Name="CNEXT" Then
AssemblyLevel=1
Else
AssemblyLevel=AssemblyLevel(oProduct.Parent.Parent)+1
End If
End Function

LukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
 
Nice solutions, a star from me.

But if you are using this script on a CATProduct coming from a PDM system (like VPM for example), you have to modify because is giving an error due to the first condition:

If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
Exit Sub
End If

Regards
Fernando

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top