Use TextCommands2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I don't understand English, so I use a translation site to create my sentences.
So, please understand that there may be inappropriate expressions.
This is a continuation of our efforts here.
https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands/m-p/9645717#M10758
I myself have a better understanding than before.
There are a number of commands that are not provided as an API, but can be used in conjunction with TextCommands.
One of them is a group of bodies.
This is illustrated by the data in this state.
Make the Bodies of RootComponent (marked in red) selected and execute the following TextCommands.
Commands.Start FusionCreateSurfaceGroupCommand
You can make a group without problems.
Then execute the TextCommands in the same way with the "Component1:1" Bodies (marked in blue) selected.
This one is also created without problems.
To perform these same processes, we created the following script.
# occ
occ :adsk.fusion.Occurrence = root.occurrences[0]
if occ.nativeObject:
print('{} has nativeObject'.format(occ.name))
else:
print('{} has not nativeObject'.format(occ.name))
createBodiesGroup(occ)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def createBodiesGroup(
root_occ :adsk.core.Base
):
# select bodies
sels :adsk.core.Selections = _ui.activeSelections
sels.clear()
sels.add(root_occ.bRepBodies)
# execute TextCommands
_app.executeTextCommand('Commands.Start FusionCreateSurfaceGroupCommand')
Unfortunately, RootComponent creates it, but Occurrence(Component1:1) results in an error and cannot be created.
I do not know the cause of this error.
(Maybe it's because I don't understand NativeObject and Proxy correctly.)
However, I found that you can select correctly by using the TextCommands here.
Selections.Add <Paths>
API's Selections.Add method can only accommodate what is provided as an object in the API, but
In the case of the TextCommands is possible to state selected, even in entity that is not provided as an object in the API.
Eventually you need to get the <paths>.
To verify the <paths>, select the Bodies of "Component1:1" in the GUI and execute the following text command.
Selections.List
The numbers displayed may be different.
Each of these numbers seems to be represented as <EntityRef><EntityID>.
We could use this one to see the properties of the entity.
PEntity.Properties <EntityRef>
Let's try it out with "57".
The return value seems to be in JSON format.
Since it is tedious to check them one by one, we will create a script like this one to check them.
# Fusion360API Python script
import adsk.core, adsk.fusion, traceback
import json
import os
import neu_server
_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
# check select count
sels :adsk.core.Selections = _ui.activeSelections
if sels.count < 1:
_ui.messageBox('Pre-select the elements to examine.')
return
if sels.count > 1:
_ui.messageBox('There should be only one entity to choose.')
return
dumpMsg('-- select info --')
# get paths
linesep = os.linesep
paths :str = _app.executeTextCommand(u'Selections.List').rstrip(linesep)
dumpMsg('<paths> : {}\n'.format(paths))
# split paths
ids = paths.split(':')
if len(ids) < 1:
return
# loop all ID
for id in ids:
try:
dumpMsg('prop -> : {}'.format(id))
# get property
# prop_txt = _app.executeTextCommand(u'PEntity.Properties {}'.format(id))
prop_txt = neu_server.get_entity_properties(id)
if len(prop_txt) < 1: continue
# to json
prop = json.dumps(prop_txt, indent=2)
dumpMsg(str(prop) + '\n')
except:
pass
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def dumpMsg(msg :str):
_ui.palettes.itemById('TextCommands').writeText(str(msg))
In my environment
prop_txt = _app.executeTextCommand(u'PEntity.Properties {}'.format(id))
with the same function because the output was not clean in
import neu_server
・・・
prop_txt = neu_server.get_entity_properties(id)
to use.
Since it is not practical to write out all the information because there is a lot of information to be output, this is what happens when you only write out the "interfaceId" key.
Also, I would like to add my prediction based on the name of the key.
57 : "Ns::Comp::ComponentInstance" - Occurrence?
3 : "Na::FusionComponent" - Component?
4 : "Ns::Comp::ComponentInstances" - Occurrences?
224 : "Ns::Comp::ComponentInstance" - Occurrence?
173 : "Na::FusionComponent" - Component?
176 : "Ns::BREP::Bodies" - bodies?
I didn't expect there to be a kind of Occurrence in the RootComponent as well, but it feels more natural.
(If you select only the root component and run the script, it only returns "57")
Also, if you look for the following entityID from each property, you will find it with a key like this
57 : "Ns::Comp::ComponentInstance"['rTargetComponent']['entityId'] -> 3
3 : "Na::FusionComponent"['rComponentBaseInstances']['entityId'] -> 4
4 : "Ns::Comp::ComponentInstances"['children']['entityId'] -> 224
224 : "Ns::Comp::ComponentInstance"['rTargetComponent']['entityId'] -> 173
173 : "Na::FusionComponent"['rBodies']['entityId'] -> 176
176 : "Ns::BREP::Bodies"
This way you have relative information.
The image in my head looks like this.
I could be wrong.
Red is the elements that can be selected in the GUI (mainly occlusion) and green is practically not shown.
Reference source (component). A collection of each element that is not even shown in blue.
I was surprised to find that the root component also has an occurrence.
Such a command also exists.
Also, entityID is
PAsset
PBody
PComponent
PDocument
PEntity
PInterfaces
PInstance
These commands are also available. (I'm sure there are others)