Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inert existing component from external app

Status
Not open for further replies.

tomtom12

Automotive
Jul 11, 2023
9
0
0
GB
Hello,

I have so far developed small application with VB NET in VisualStudio which allows browsing parts and their details from SQL database. This also contains links to files, including CATpart.

Naturally I came to this idea that it would be nice o have a button that would send the CATpart file into 'Insert> Existing component' function.

Question is where to start and where to find documentation for this? Or maybe even close examples?

My understanding is that the external App would send full path to the actual CATpart file, while script automates what user would normally do (click Insert -> Existing component -> browse folders to find the part).

I do not need ( at least at this stage) any other functionality (i.e. happy to keep manual selection of the top level of the assembly or any later dialogs that appear after loading the part in ).

Any help would me much appreciated.
 
Replies continue below

Recommended for you

Thanks for your reply.

Could you expand a little more where this is described? Is it the Catia's internal scripting interface documentation? Ideally I am after some samples such functionality can be achieved.
 
So I found following code:

Code:
Sub LoadDocumentToProduct()

Dim Document As ProductDocument

Set Document = CATIA.ActiveDocument

Dim MainProduct As Product

Set MainProduct = Document.Product

Set Prod = MainProduct.Products

Dim arrayOfVariantOfBSTR( 0) ' Quantity loaded documents-1

arrayOfVariantOfBSTR(0) = "document full path"

Prod.AddComponentsFromFiles arrayOfVariantOfBSTR, "All"

End Sub

But this is fairly old one, I had to drop 'Set' statements and create Dim for Prod (which I am unsure about), but running the script I get the error that Document is not part of the Product (I will confirm the error later).

Does anyone have this running in much newer VB?
 
It is in Automation documentation indeed. It's public and presented in V5Automation.chm file.

Use CATIA.Documents.Read to load PartDocument from disk and Products.AddComponent to add it's root Product to your current one.
 
I managed to get the scrip to work, but failing to find any API for the function 'Insert Existing Component with positioning' - is this even possible with external script?
 
It would have saved time if you had mentioned positioning in the first place.

There's no direct API to do this although it's possible to move and rotate inserted part with Product.Position.SetComponents
 
My understanding is that Product.Position.SetComponents would specify specific position while in ideal case I would like to call up Smart Move function for the added component.

I have managed to use script to add parts into Root but now stuck of how to apply function for last inserted item. Any clues?
 
So I found a way to do this - basically, script is searching for the component name in the active document and listing number of instances into collection, then finding last item number from this collection and use it to search again using part number + instance ID. Here's the code that took me whole three days to develop (I'm not software engineer and this is my first project):

Code:
                Dim mySel As Selection
                mySel = CATIA.ActiveDocument.Selection

                Dim selString As String
                selString = "Name='yourPNgoeshere',all"
                mySel.Clear()
                mySel.Search(selString) 'Select ALL matching products

                Dim i As Integer
                Dim sel1 As Selection
                sel1 = CATIA.ActiveDocument.Selection

                If mySel.Count > 0 Then 'Count all items that named under "selString" value
                    Dim aList(mySel.Count)
                    For i = 1 To mySel.Count
                        aList(i - 1) = mySel.Item(i).Value
                    Next
                    Dim InstanceNumber
                    InstanceNumber = (aList.Count - 1) 'Count instances and subtract 1 because VBA starts at 0
                    mySel.Clear() 'Clear selection
                    Call sel1.Search("Name='yourPNgoeshere'" & "." & InstanceNumber & ",all")
                    CATIA.StartCommand("Smart Move") 'Start "Smartmove" command in Catia
                Else
                    Dim FirstInstanceNumber
                    FirstInstanceNumber = "1" 'Assigning value "1" because there is only one such component in the assembly
                    Call sel1.Search("Name='yourPNgoeshere'" & "." & FirstInstanceNumber & ",all")
                    CATIA.StartCommand("Smart Move") 'Start "Smartmove" command in Catia
                End If
 
Status
Not open for further replies.
Back
Top