Text string in sketch that takes component name

Text string in sketch that takes component name

JYZMT
Advocate Advocate
2,168 Views
7 Replies
Message 1 of 8

Text string in sketch that takes component name

JYZMT
Advocate
Advocate

Is it possible to run a sketch which pushes a file name and component name into a text string within a sketch? 

 

So say there was a sketch with a string in it of VWXYZ. When the script is run, it searches for that string, and replace it with "file name_component name"? Or something?

 

Maybe there is a better way. But I don't know of a way of getting the script to know which bit of text to over ride?

 

Any thoughts on this would help, Thanks

Rob

0 Likes
Accepted solutions (1)
2,169 Views
7 Replies
Replies (7)
Message 2 of 8

JesusFreke
Advocate
Advocate

 

I think something like this should work (note: untested code 🙂

 

for component in adsk.core.Application.get().activeProduct.allComponents:
    for sketch in component.sketches:
        for sketchText in sketch.sketchTexts:
            if sketchText.text == "REPLACE_ME":
                sketchText.text = "some other string"

            

Message 3 of 8

JYZMT
Advocate
Advocate

Ok, thanks. I'm assuming there a bit I need to put in the 

"some other string"

which is:

[get file name ] concatenation [get component name]

But clearly not written as the above, but written in a way the scripting language understands.

Thanks so much for your help so far. 

 

0 Likes
Message 4 of 8

JesusFreke
Advocate
Advocate
Accepted solution

You can get the name of the active document from adsk.core.Application.get().activeDocument.name, and the name of the component from component.name. If you need help building the string, see, e.g. here.

Message 5 of 8

JYZMT
Advocate
Advocate

Thanks, I'll mess about with the above, see how I get on. 

 

0 Likes
Message 6 of 8

JYZMT
Advocate
Advocate

Thanks for this. Is there a way to get 

adsk.core.Application.get().activeDocument.name

to drop the version number v1, v2 etc from the end? 

0 Likes
Message 7 of 8

JesusFreke
Advocate
Advocate

I think this is what you want

 

adsk.core.Application.get().activeDocument.dataFile.name

 

0 Likes
Message 8 of 8

jackalopes
Participant
Participant

I'm looking to do this also, but I am new to coding within Fusion360.  Where would I place this code and how would I call in within a User Parameter?

0 Likes