TechDraw: scripting

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
drmacro
Veteran
Posts: 8983
Joined: Sun Mar 02, 2014 4:35 pm

TechDraw: scripting

Post by drmacro »

Attempting to use the python console to look at objects in TechDraw page.

When I click on a circle in the view the selected circle shows as Edge4.

If I then type in the python console:

Code: Select all

myObj = FreeCAD.ActiveDocument.getObject('Edge4')
print(myObj.TypeID)
The result is:

Code: Select all

Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'TypeID'
This is right off the FreeCAD Scriton Basics web page.

Since it doesn't work for objects from the model view either, I'm assuming I'm missing loading some python module, or some other detail I missed in that webpage. :oops:
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
Roy_043
Veteran
Posts: 8551
Joined: Thu Dec 27, 2018 12:28 pm

Re: TechDraw: scripting

Post by Roy_043 »

Code: Select all

FreeCAD.ActiveDocument.View.getEdgeByIndex(4)

Code: Select all

FreeCAD.ActiveDocument.getObject('View').getEdgeByIndex(4)
Note: 'View' is the internal name of the TechDraw view.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: TechDraw: scripting

Post by vocx »

drmacro wrote: Tue Aug 04, 2020 2:26 pm ...

Code: Select all

myObj = FreeCAD.ActiveDocument.getObject('Edge4')
print(myObj.TypeID)
Edges are not objects. Edges are subelements inside objects.
This is right off the FreeCAD Scriton Basics web page.
Which one? We can't guess.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
drmacro
Veteran
Posts: 8983
Joined: Sun Mar 02, 2014 4:35 pm

Re: TechDraw: scripting

Post by drmacro »

vocx wrote: Tue Aug 04, 2020 4:22 pm
drmacro wrote: Tue Aug 04, 2020 2:26 pm ...

Code: Select all

myObj = FreeCAD.ActiveDocument.getObject('Edge4')
print(myObj.TypeID)
Edges are not objects. Edges are subelements inside objects.
So, when I click on a circle in a TechDraw page view the status bar shows something like:

Code: Select all

<doc name>.ProjItem006.Edge4
How do I find the coordinates of the center of the circle and the radius?

This is right off the FreeCAD Scriting Basics web page.
Which one? We can't guess.
Hmm...I guess I thought mentioning the Official FreeCAD documentation would kinda place it in the world... :oops: My bad.

FreeCAD Scripting Basics - Querying objects
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
Roy_043
Veteran
Posts: 8551
Joined: Thu Dec 27, 2018 12:28 pm

Re: TechDraw: scripting

Post by Roy_043 »

Code: Select all

edge = FreeCAD.ActiveDocument.View002.getEdgeByIndex(0)
edge.Curve.Radius
edge.Curve.Location
The location seems to be relative to the center of the view.
drmacro
Veteran
Posts: 8983
Joined: Sun Mar 02, 2014 4:35 pm

Re: TechDraw: scripting

Post by drmacro »

Roy_043 wrote: Tue Aug 04, 2020 8:26 pm

Code: Select all

edge = FreeCAD.ActiveDocument.View002.getEdgeByIndex(0)
edge.Curve.Radius
edge.Curve.Location
The location seems to be relative to the center of the view.
Yeah I eventual put that together.

My ultimate goal is to get the info on a user selected object (so I was cheating by knowing the index 8-) )
So far that has been elusive. :roll:
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
vanuan
Posts: 539
Joined: Wed Oct 24, 2018 9:49 pm

Re: TechDraw: scripting

Post by vanuan »

drmacro wrote: Tue Aug 04, 2020 9:28 pm My ultimate goal is to get the info on a user selected object (so I was cheating by knowing the index 8-) )
So far that has been elusive. :roll:
Maybe you want this:

Code: Select all

sel = FreeCADGui.Selection.getSelection()[0]
sel.Name
# View
sel.TypeId
# 'TechDraw::DrawViewPart'
This is will return an object selected in a tree view.

This is usually a 2D view or a projection of an actual 3D object. So, to get the object:

Code: Select all

sel.Source[0].TypeId
# 'Part::Box'
Or you want information on the selected edge?

Code: Select all

sel = FreeCADGui.Selection.getSelectionEx()[0]
sel.FullName
# "(App.getDocument('Unnamed').getObject('View'),['Edge1',])"
sel.SubElementNames
# ('Edge1',)
edgeIndex = sel.SubElementNames[0].split('Edge')[1]
# 1
sel.Object.getEdgeByIndex(edgeIndex)
User avatar
wandererfan
Veteran
Posts: 6317
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: TechDraw: scripting

Post by wandererfan »

drmacro wrote: Tue Aug 04, 2020 9:28 pm so I was cheating by knowing the index
As @vanuan said, SelectionEx is your friend here. Just remember that names ("Edge1") start at 1 and indexes start at 0.
User avatar
vanuan
Posts: 539
Joined: Wed Oct 24, 2018 9:49 pm

Re: TechDraw: scripting

Post by vanuan »

vocx wrote: Tue Aug 04, 2020 4:22 pm
This is right off the FreeCAD Scriton Basics web page.
Which one? We can't guess.
He meant this page: https://wiki.freecadweb.org/FreeCAD_Scripting_Basics

I find it confusing too that there's no clear separation between parametric objects and the computed geometry.

Maybe there should be a separate tree panel that shows faces, edges, vertices of a selected object.
I think the very ability to select sub-elements without dedicated keyboard modifiers is harmful. It doesn't educate the newbie user about the topo-naming issue. And there's usually little you can do with selected geometry. So maybe by default, only objects should be selectable. Or there should be selection equivalent in the tree panel.
User avatar
vanuan
Posts: 539
Joined: Wed Oct 24, 2018 9:49 pm

Re: TechDraw: scripting

Post by vanuan »

wandererfan wrote: Wed Aug 05, 2020 1:25 am As @vanuan said, SelectionEx is your friend here. Just remember that names ("Edge1") start at 1 and indexes start at 0.
That's unfortunate. There should be another way to identify selected sub-elements, without relying on the hacky name parsing.
Post Reply