[Python] Afficher le nom d'un objet

Forum destiné aux questions et discussions en français
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
Post Reply
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

[Python] Afficher le nom d'un objet

Post by mario52 »

Bonjour
1: je voudrais savoir comment obtenir le nom d'un objet, pas si c'est un " Line " ou " Circle " mais le nom affiché dans la Vue combinée même après renommage

2: je voudrais savoir aussi si on peut donner un nom à un objet que l'on crée Line, Circle ... dé sa création dans un script comme pour les notes.
merci
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: [Python] Afficher le nom d'un objet

Post by mario52 »

Bonjour
j'ai trouvé une alternative avec le Sketcher

Code: Select all

App.activeDocument().addObject('Sketcher::SketchObject','Nom_Au_Choix')
App.ActiveDocument.Nom_Au_Choix.addGeometry(Part.Line(App.Vector(-10,0.0,0.0),App.Vector(20.0,0.0,0.0)))
App.ActiveDocument.recompute()
mais je suis toujours preneur pour les autres solutions du premier post.
merci
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: [Python] Afficher le nom d'un objet

Post by mario52 »

Bonjour
Ce code permet d'afficher tous les objets du projet.

Code: Select all

# -*- coding: utf-8 -*-
#liste les objets du projet en cours
objs = FreeCAD.ActiveDocument.Objects
#print objs
for obj in objs:
    name = obj.Name
    print name              # Affiche le nom de l'objet
#    doc.removeObject(name) # Efface l'objet
mais dans la feuille de mise en plan, les pages sont nommées par
Ortho (TopView)
Ortho001 (FrontView)
Ortho002 (RightView)
comment afficher le nom que l'on voit dans la Vue Combinée c'est à dire , TopView, FrontView, RightView
merci
mario

Solution
.Label = TopView (nom modifiable)
.Name = Ortho (nom interne non modifiable)

Code: Select all

>sel = FreeCADGui.Selection.getSelection()   
>object_Label = sel[0].Label
>object_Name  = sel[0].Name
>print object_Label
Line_205
>print object_Name
Line005002001002005003004021
in the macro you use .Name

Code: Select all

>>> FreeCADGui.getDocument("pente03").getObject("Line005002001002005003004021").LineColor = (0.03137255,0.38039216,0.58039218)
Not .Label

Code: Select all

>>> FreeCADGui.getDocument("pente03").getObject("Line_205").LineColor = (0.03137255,0.38039216,0.58039218)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'LineColor'
[edit 30/12/2013] ajout de la solution !
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Post Reply