How to assign an existing tool to an operation without duplicating the tool?

How to assign an existing tool to an operation without duplicating the tool?

marc.blondin
Explorer Explorer
347 Views
2 Replies
Message 1 of 3

How to assign an existing tool to an operation without duplicating the tool?

marc.blondin
Explorer
Explorer

Hello Fusionists,

I am writing a CAM script that duplicates an operation and modifies the parameters of the new operation based on the values of the previous one. It works alright, but there is one thing that drives me nuts:

The new operation gets its tool reassigned to the next one in the current Document Tool Library. But every time this occurs, a new instance of the tool is created in the current Document Tool Library instead of just reusing the one that already exists.

 

Here's the code extract in question:

 
currentOp = currentSetup.operations.item(i)
toolNb = currentOp.tool.parameters.itemByName('tool_number').value.value
currentOp.duplicate()
i = i+1
newOp = currentSetup.operations.item(i)
newOp.tool = cam.documentToolLibrary.item(toolNb+1)

 

So let's say that operation#1 was using T2, when operation#2 (the duplicate) gets assigned T3, my Document Tool Library now contains two identical T3 tools.

 

I banged my head against this thing for a few hours yesterday and finally gave up and asked our favorite chatbot for some help and I was told that this is the way it is... when assigning a tool from the current document with the API, a duplicate will always be created no matter what. I couldn't find any mention of this in the documentation...

 

Is there a workaround for this?

Much thanks!

0 Likes
348 Views
2 Replies
Replies (2)
Message 2 of 3

boopathi.sivakumar
Autodesk
Autodesk

Hi @marc.blondin 

I think the code you have added does not give me what you excatly want

currentOp = currentSetup.operations.item(i)
toolNb = currentOp.tool.parameters.itemByName('tool_number').value.value
..
.
## This line 
newOp.tool = cam.documentToolLibrary.item(toolNb+1)

The last line seems wrong to me. the toolNb is a actual tool number parameter of the tool 

but you are using that to find the item in the document tool libray that seems to be wrong it will always assign wrong tool.


Ideally when you just duplicate the toolpath it won't create the new instance of tool. So I am not sure excatly what is the need.

 

But I am assuming if you want to use the same tool with other operation for some other operation.

then if you try to do like

op2.tool = op1.tool 

 

Then op1.tool will always make a copy of the same instance it won't reference 

so you have use Query something like this 

        toolQuery = cam.documentToolLibrary.createQuery()
        toolQuery.criteria.add('tool_number', adsk.core.ValueInput.createByReal(toolNb))
        tool = toolQuery.execute()
        newOp.tool = tool[0].tool

 


Boopathi Sivakumar
Senior Technology Consultant

0 Likes
Message 3 of 3

marc.blondin
Explorer
Explorer

Hi @boopathi.sivakumar ,

Thanks for the reply.

 

"But I am assuming if you want to use the same tool with other operation for some other operation." => Not exactly... I want to use the next Tool# after the one used in the previous operation.

 

You are right about the tool number parameter. I simplified the code a bit, but in the actual script I link the Tool# to the tool item #. The tool number assigning works without problem. My only problem is that when it assigns the next tool to the duplicated operation, it creates a new tool instance in my document library instead of simply assigning an existing tool.

 

For example:

I have an existing document with a Document Tool Library containing T1, T2, T3.

I have Op#2 that uses T2.

The script duplicates Op#2 and renames it to Op#3 (works well without duplicating T2), changes a few CAMparameters based on the values of Op#2 (works also perfectly) then it increments the tool used from T2 to T3 (which seems to duplicate T3 instead of using the one already existing in the Document Tool Library).

After running the script, I have 2 instances of T3 in the library:

demo.png

 

I am going to experiment with the query like you suggested, this is something I didn't know existed (I am not much of a coder...!)

Thank you for the suggestion,

Marc

0 Likes