Gestion d'une fenetre et des variables destinées au dessin

Forum destiné aux questions et discussions en français
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
User avatar
blonblon
Posts: 253
Joined: Sat Sep 24, 2016 6:06 pm
Location: Uzes (Gard), France

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by blonblon »

Merci pour tes conseils openBrain

Voici le code qui fonctionne.

[code]

#-*- coding: utf-8 -*-

import Draft, Part
import sys
import math
import Part
import sys
import FreeCAD, FreeCADGui
import Sketcher, PartDesign
import ProfileLib.RegularPolygon
from FreeCAD import Base
from PySide import QtCore, QtGui

# t1 simule la case a cocher d'un checbox
t1 = 1 #1 ou 0

rayon_poulie = 12.5

# ici on insére le teste de présence nouveau fichier part body sketch
myDoc = App.ActiveDocument
if myDoc == None: #il n'y a pas de Document on le crée
myDoc = App.newDocument("Pignon")
############
objs = [obj for obj in myDoc.Objects if str(obj) == "<Part object>"]
if len(objs) == 0: #Il n'y a pas de Part on le crée
myPart = myDoc.addObject('App::Part','Part')
myPart.Label = "Pièce"
else: #Il y a (au moins) un Part dans le document
myPart = objs[0] #On utilise par ex. le premier Part dans la suite
############
objs = [obj for obj in myDoc.Objects if str(obj) == "<body object>"]
if t1 == 0:
if len(objs) == 0:
myBody = myDoc.addObject('PartDesign::Body','Body')
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
else:
myBody = myDoc.addObject('PartDesign::Body','Body')
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
if t1 == 1:
if len(objs) == 0:
myBody = myDoc.addObject('PartDesign::Body','Body')
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
else:
myBody = [obj for obj in myDoc.Objects if str(obj) == "<body object>"][-1]
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
############
###
# à partir d'ici on insére le code du dessin
###
# On place le cercle qui va servir pour le pignon en GT2, GT3, ou GT5
mySketch.addGeometry(Part.Circle(App.Vector(-0.959298,0.986822,0),App.Vector(0,0,1),rayon_poulie),False)
mySketch.addConstraint(Sketcher.Constraint('Coincident',-1,1,0,3))
mySketch.addConstraint(Sketcher.Constraint('Radius',0,rayon_poulie+1))
mySketch.setDatum(1,App.Units.Quantity(str(rayon_poulie)+' mm'))
############
mySketch1.addGeometry(Part.Circle(App.Vector(-0.959298,0.986822,0),App.Vector(0,0,1),rayon_poulie),False)
mySketch1.addConstraint(Sketcher.Constraint('Coincident',-1,1,0,3))
mySketch1.addConstraint(Sketcher.Constraint('Radius',0,rayon_poulie+1))
mySketch1.setDatum(1,App.Units.Quantity(str(rayon_poulie)+' mm'))
############

[/code]

Je vais finir mes recherches pour les poulies T5 afin d'avoir un dessin correcte, et finir le programme.
Last edited by blonblon on Thu Jul 04, 2019 7:33 am, edited 1 time in total.
User avatar
blonblon
Posts: 253
Joined: Sat Sep 24, 2016 6:06 pm
Location: Uzes (Gard), France

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by blonblon »

Apparemment les balises de code ne fonctionne pas
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by openBrain »

blonblon wrote: Thu Jul 04, 2019 7:32 am Apparemment les balises de code ne fonctionne pas
Oui, c'est bizarre. Du coup l'indentation est partie et le code ne marchera pas si quelqu'un le copie-colle. ;)

Si ça t'intéresse, on peut analyser un peu cette partie du code :

Code: Select all

############
objs = [obj for obj in myDoc.Objects if str(obj) == "<body object>"]
if t1 == 0:
if len(objs) == 0:
myBody = myDoc.addObject('PartDesign::Body','Body')
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
else:
myBody = myDoc.addObject('PartDesign::Body','Body')
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
if t1 == 1:
if len(objs) == 0:
myBody = myDoc.addObject('PartDesign::Body','Body')
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
else:
myBody = [obj for obj in myDoc.Objects if str(obj) == "<body object>"][-1]
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
Si on regarde :
  • Les 2 sketchs sont créés de la même façon et quoi qu'il arrive. Donc on peut factoriser en dehors des conditions "if"
  • Pour ce qui est du Body, il n'y a qu'un cas où tu utilises le dernier Body (au lieu d'en créer un) : quand "t1 == 1" et que "len(objs) != 0"
Donc, toute la portion au dessus peut se simplifier comme ça :

Code: Select all

############
objs = [obj for obj in myDoc.Objects if str(obj) == "<body object>"]
if t1 == 1 and len(objs) != 0:
    myBody = [obj for obj in myDoc.Objects if str(obj) == "<body object>"][-1]
else:
    myBody = myDoc.addObject('PartDesign::Body','Body')
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
Plus concis, non ?
User avatar
blonblon
Posts: 253
Joined: Sat Sep 24, 2016 6:06 pm
Location: Uzes (Gard), France

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by blonblon »

Merci beaucoup openBrain

Je viens de prendre une leçon, et je pense qu'une partie des lecteurs de ce Post en profiteront aussi.

Je viens de tester ça marche nickel :P

Je vais continuer il y aura surement encore des problèmes. :D
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by openBrain »

blonblon wrote: Thu Jul 04, 2019 11:59 am Je vais continuer il y aura surement encore des problèmes. :D
Pas vraiment des problèmes, mais des choses simplifiables. Ce qui est appréciable quand le code grossit. :)
En tout cas c'est cool si tu as compris la démarche pour pouvoir la reproduire. ;)
En relisant le code au-dessus, je m'aperçois même qu'il est un peu stupide parce qu'il fait 2 fois la même chose. Il est encore plus simple et plus logique comme ça :

Code: Select all

############
objs = [obj for obj in myDoc.Objects if str(obj) == "<body object>"]
if t1 == 1 and len(objs) != 0:
    myBody = objs[-1] ##Le changement est ici
else:
    myBody = myDoc.addObject('PartDesign::Body','Body')
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
User avatar
blonblon
Posts: 253
Joined: Sat Sep 24, 2016 6:06 pm
Location: Uzes (Gard), France

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by blonblon »

J'ai du modifier les testes une condition ne s'exécutait pas, j'ai bien bataillé mais je pense y etre arrivé.

Code: Select all

   objs = [obj for obj in myDoc.Objects if str(obj) == "<body object>"]
   if len(objs) == 0:
      myBody = myDoc.addObject('PartDesign::Body','Body')
   elif t1.isChecked(): # and len(objs) != 0: Unchecked Checked
      myBody = myDoc.addObject('PartDesign::Body','Body')
   else:
      myBody = objs[ - 1]
   mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
   mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
Il y a bien sur de nouveaux problèmes qui ne demandent qu'à etre résolus :lol: :lol:
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by openBrain »

blonblon wrote: Fri Jul 12, 2019 8:34 pm J'ai du modifier les testes une condition ne s'exécutait pas, j'ai bien bataillé mais je pense y etre arrivé.
Du coup à la vue de ton code, je pense que tu peux simplifier à fond comme suit ;)

Code: Select all

   objs = [obj for obj in myDoc.Objects if str(obj) == "<body object>"]
   if len(objs) != 0 and t1.isChecked() == False:
      myBody = objs[ - 1]
   else:
      myBody = myDoc.addObject('PartDesign::Body','Body')
   mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
   mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
User avatar
blonblon
Posts: 253
Joined: Sat Sep 24, 2016 6:06 pm
Location: Uzes (Gard), France

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by blonblon »

Merci openBrain ça fonctionne

J'ai encore un probléme dans ces lignes de code

Code: Select all

Gui.getDocument(myDoc).getObject("Sketch").Visibility=False
Gui.getDocument(myDoc).getObject("Pad").Visibility = False
Gui.getDocument(myDoc).getObject("Pocket").Visibility=False
Gui.getDocument(myDoc).getObject("PolarPattern").Visibility = True
comment récuperer ("Sketch") ou ("Pad") ou("Pocket") ou ("PolarPattern") qui est en cour à chaque nouvelle création
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by openBrain »

blonblon wrote: Sat Jul 13, 2019 7:41 am Merci openBrain ça fonctionne

J'ai encore un probléme dans ces lignes de code
Là il va falloir plus de contexte. :)
Les lignes que tu montres rendent invisible un certain nombre d'objets.
Si je repars à peu près du code qui était ici :
openBrain wrote: Sun Jun 30, 2019 11:35 am
L'idée, comme les objets sont enregistrés au fur et à mesure, c'est d'utiliser les variables comme ça :

Code: Select all

myPad.ViewObject.Visibility = False
myPocket.ViewObject.Visibility = False
...
User avatar
blonblon
Posts: 253
Joined: Sat Sep 24, 2016 6:06 pm
Location: Uzes (Gard), France

Re: Gestion d'une fenetre et des variables destinées au dessin

Post by blonblon »

Le code fonctionne ça active ou désactive la visibilité des divers éléments.

Il me reste à trouver une astuce pour que les poulies s'empilent.
Post Reply