Gestion d'une fenetre et des variables destinées au dessin
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
Be nice to others! Read the FreeCAD code of conduct!
Re: Gestion d'une fenetre et des variables destinées au dessin
Effectivement on peut supprimer la fonction __init__().
J'ai des notions de programmation en visual basic pour la suite Office, en Delphi et surtout en C++ Builder6, on trouve beaucoup d'aide pour ces programmes, ces notions aident un peu, mais ça ne suffit pas l'écriture Python Freecad est différente de ce que je connais, et il n'y a pas beaucoup de choses pour aider à part le Forum, heureusement .
J'ai des notions de programmation en visual basic pour la suite Office, en Delphi et surtout en C++ Builder6, on trouve beaucoup d'aide pour ces programmes, ces notions aident un peu, mais ça ne suffit pas l'écriture Python Freecad est différente de ce que je connais, et il n'y a pas beaucoup de choses pour aider à part le Forum, heureusement .
Re: Gestion d'une fenetre et des variables destinées au dessin
Le programme fonctionne pour des poulies GT2 GT3, pour les poulies GT5 le programme fonctionne mais n'est pas complet il manque la ou les valeurs des arrondis sur les angles.
Dans l'etat actuel du programme on ne peut dessiner qu'un seul pignon, je n'ai pas encore de solution pour pour ajouter d'autre pignons
Code: Select all
#-*- coding: utf-8 -*-
#
# Dessin pignon GT2
#
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
__DBG__ = False #True #Mettre à False pour supprimer la sortie de debug
def pdbg(text):
if __DBG__: #Si on est en mode debug
App.Console.PrintMessage(text)
def proceed():
###
# On teste le type de poulie
Type = t0.currentText()
###
# Données nécessaires au dessin poulie GT2
if 'GT2' == Type:
pdbg('Type = ' + str(Type)+"\n")
nombre_de_dents = float(s1.text())
pdbg('Nb dents = ' + str(nombre_de_dents)+"\n")
pas = 2
pdbg('Pas = ' + str(pas)+"\n")
PLD = 0.254
pdbg('PLD = ' + str(PLD)+"\n")
h = 0.75
pdbg('h = ' + str(h)+"\n")
circonference = nombre_de_dents * pas
pdbg('circonference = ' + str(circonference)+"\n")
diametre_primitif = circonference / math.pi
pdbg('diametre_primitif = ' + str(diametre_primitif)+"\n")
diametre_poulie = diametre_primitif - (PLD * 2)
pdbg('diametre_poulie = ' + str(diametre_poulie)+"\n")
rayon_poulie = diametre_poulie / 2
pdbg('rayon_poulie = ' + str(rayon_poulie)+"\n")
largeur_poulie = float(s2.text())
pdbg('largeur_poulie = ' + str(largeur_poulie)+"\n")
R1 = 0.15
pdbg('rayon_R1 = ' + str(R1)+"\n")
R2 = 1
pdbg('rayon_R2 = ' + str(R2)+"\n")
R3 = 0.555
pdbg('rayon_R3 = ' + str(R3)+"\n")
b = 0.4
b2 = b*2
elif 'GT3' == Type:
pdbg('Type = ' + str(Type)+"\n")
nombre_de_dents = float(s1.text())
pdbg('Nb dents = ' + str(nombre_de_dents)+"\n")
pas = 3
pdbg('Pas = ' + str(pas)+"\n")
PLD = 0.381
pdbg('PLD = ' + str(PLD)+"\n")
h = 1.14
pdbg('h = ' + str(h)+"\n")
circonference = nombre_de_dents * pas
pdbg('circonference = ' + str(circonference)+"\n")
diametre_primitif = circonference / math.pi
pdbg('diametre_primitif = ' + str(diametre_primitif)+"\n")
diametre_poulie = diametre_primitif - (PLD * 2)
pdbg('diametre_poulie = ' + str(diametre_poulie)+"\n")
rayon_poulie = diametre_poulie / 2
pdbg('rayon_poulie = ' + str(rayon_poulie)+"\n")
largeur_poulie = float(s2.text())
pdbg('largeur_poulie = ' + str(largeur_poulie)+"\n")
R1 = 0.25
pdbg('rayon_R1 = ' + str(R1)+"\n")
R2 = 1.52
pdbg('rayon_R2 = ' + str(R2)+"\n")
R3 = 0.85
pdbg('rayon_R3 = ' + str(R3)+"\n")
b = 0.61
b2 = b*2
elif 'GT5' == Type:
pdbg('Type = ' + str(Type)+"\n")
nombre_de_dents = float(s1.text())
pdbg('Nb dents = ' + str(nombre_de_dents)+"\n")
pas = 5
PLD = 0.43
pdbg('PLD = ' + str(PLD)+"\n")
h = 1.2
pdbg('h = ' + str(h)+"\n")
circonference = nombre_de_dents * pas
pdbg('circonference = ' + str(circonference)+"\n")
diametre_primitif = circonference / math.pi
pdbg('diametre_primitif = ' + str(diametre_primitif)+"\n")
diametre_poulie = diametre_primitif - (PLD * 2)
pdbg('diametre_poulie = ' + str(diametre_poulie)+"\n")
rayon_poulie = diametre_poulie / 2
pdbg('rayon_poulie = ' + str(rayon_poulie)+"\n")
largeur_poulie = float(s2.text())
pdbg('largeur_poulie = ' + str(largeur_poulie)+"\n")
largeurDent = 2.65
angleDent = 40
###
# 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" + Type)
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 len(objs) == 0: #Il n'y a pas de Body on le crée
myBody = myDoc.addObject('PartDesign::Body','Body')
myBody.Label = "Corp"
App.getDocument("Pignon" + Type).getObject("Part").addObject(App.getDocument("Pignon" + Type).getObject("Body"))
else: #Il y a (au moins) un Body dans le Part
myBody = objs[0] #On utilise par ex. le premier Body dans la suite
objs = [obj for obj in myDoc.Objects if str(obj) == "<Sketcher::SketchObject>"]
if len(objs) == 0: #Il n'y a pas de Sketch on le crée
myDoc.Body.newObject('Sketcher::SketchObject','Sketch')
myDoc.Sketch.Support = (App.activeDocument().XY_Plane001, [''])
myDoc.Sketch.MapMode = 'FlatFace'
sketchNo = App.ActiveDocument.getObject('Sketcher::SketchObject')
print(sketchNo)
else: #Il y a (au moins) un Sketch dans le Body
sketchNo = App.ActiveDocument.getObject('Sketcher::SketchObject')
print(sketchNo)
pass # Par ex. on ne fait rien :)
###
# ici on insére le code du dessin
##
# On place le cercle qui va servir pour le pignon en GT2, GT3, ou GT5
App.ActiveDocument.Sketch.addGeometry(Part.Circle(App.Vector(-0.959298,0.986822,0),App.Vector(0,0,1),rayon_poulie),False)
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',-1,1,0,3))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Radius',0,rayon_poulie+1))
App.ActiveDocument.Sketch.setDatum(1,App.Units.Quantity(str(rayon_poulie)+' mm'))
# On extrude le cercle
App.activeDocument().Body.newObject("PartDesign::Pad","Pad")
App.activeDocument().Pad.Profile = App.activeDocument().Sketch
App.activeDocument().Pad.Length = largeur_poulie
App.ActiveDocument.Pad.Length = largeur_poulie
App.ActiveDocument.Pad.Length2 = 100.000000
App.ActiveDocument.Pad.Type = 0
App.ActiveDocument.Pad.UpToFace = None
App.ActiveDocument.Pad.Reversed = 0
App.ActiveDocument.Pad.Midplane = 0
App.ActiveDocument.Pad.Offset = 0.000000
App.ActiveDocument.recompute()
Gui.getDocument("Pignon" + Type).getObject("Sketch").Visibility=False
if 'GT2' == Type or 'GT3' == Type:
# On dessine une dent GT2 ou GT3
App.activeDocument().Body.newObject('Sketcher::SketchObject','Sketch001')
App.activeDocument().Sketch001.Support = (App.ActiveDocument.Pad,["Face3"])
App.activeDocument().Sketch001.MapMode = 'FlatFace'
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch001.addGeometry(Part.LineSegment(App.Vector(-0.193479,5.124362,0),App.Vector(0.192029,5.124362,0)),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Symmetric',0,1,0,2,-2))
App.ActiveDocument.Sketch001.toggleConstruction(0)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceX',0,1,0,2,b2)) # Entraxe rayon R2
App.ActiveDocument.Sketch001.setDatum(1,App.Units.Quantity(str(b2) + ' mm')) # Entraxe rayon R2
geoList = []
geoList.append(Part.LineSegment(App.Vector(-0.733822,4.858931,0),App.Vector(0.675494,4.858931,0)))
geoList.append(Part.LineSegment(App.Vector(0.675494,4.858931,0),App.Vector(0.675494,4.707256,0)))
geoList.append(Part.LineSegment(App.Vector(0.675494,4.707256,0),App.Vector(-0.733822,4.707256,0)))
geoList.append(Part.LineSegment(App.Vector(-0.733822,4.707256,0),App.Vector(-0.733822,4.858931,0)))
App.ActiveDocument.Sketch001.addGeometry(geoList,False)
conList = []
conList.append(Sketcher.Constraint('Coincident',1,2,2,1))
conList.append(Sketcher.Constraint('Coincident',2,2,3,1))
conList.append(Sketcher.Constraint('Coincident',3,2,4,1))
conList.append(Sketcher.Constraint('Coincident',4,2,1,1))
conList.append(Sketcher.Constraint('Horizontal',1))
conList.append(Sketcher.Constraint('Vertical',2))
conList.append(Sketcher.Constraint('Vertical',4))
App.ActiveDocument.Sketch001.addConstraint(conList)
App.ActiveDocument.Sketch001.toggleConstruction(4)
App.ActiveDocument.Sketch001.toggleConstruction(1)
App.ActiveDocument.Sketch001.toggleConstruction(3)
App.ActiveDocument.Sketch001.toggleConstruction(2)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceY',3,2,1,1,R1)) # Arc cercle R1
App.ActiveDocument.Sketch001.setDatum(9,App.Units.Quantity(str(R1) + ' mm')) # Arc cercle R1
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Symmetric',3,2,2,2,-2))
App.ActiveDocument.Sketch001.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(-0.661711,4.752790,0),App.Vector(0,0,1),0.192858),0.236851,1.887364),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',3,2,5,3))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',1,1,5,2))
App.ActiveDocument.Sketch001.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.450575,5.100380,0),App.Vector(0,0,1),1.036934),-2.747505,-2.448713),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',0,2,6,3))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Tangent',5,1,6,1))
App.ActiveDocument.Sketch001.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.068227,4.563197,0),App.Vector(0,0,1),0.468263),-2.587285,-1.242243),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Tangent',6,2,7,1))
App.ActiveDocument.Sketch001.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.668608,4.825469,0),App.Vector(0,0,1),0.204199),1.190289,2.657114),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',8,3,2,2))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',1,2,8,1))
App.ActiveDocument.Sketch001.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(-0.465797,5.119339,0),App.Vector(0,0,1),1.025216),-0.712926,-0.365625),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',9,3,0,1))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Tangent',8,2,9,2))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Tangent',7,2,9,1))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Radius',6,R2)) # Arc cercle R2
App.ActiveDocument.Sketch001.setDatum(21,App.Units.Quantity(str(R2) + ' mm')) # Arc cercle R2
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Radius',7,R3)) # Arc cercle R3
App.ActiveDocument.Sketch001.setDatum(22,App.Units.Quantity(str(R3) + ' mm')) # Arc cercle R3
App.ActiveDocument.Sketch001.addGeometry(Part.Point(App.Vector(-0.150355,4.112466,0)))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('PointOnObject',10,1,-2))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('PointOnObject',10,1,7))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceY',10,1,8,1,h)) # Hauteur
App.ActiveDocument.Sketch001.setDatum(25,App.Units.Quantity(str(h) + ' mm'))
App.ActiveDocument.Sketch001.addExternal("Pad","Edge3")
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('PointOnObject',5,2,-3))
App.ActiveDocument.Sketch001.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.298317,3.331532,0),App.Vector(0,0,1),1.930821),1.288327,2.078132),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',5,2,11,2))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',8,1,11,1))
App.ActiveDocument.Sketch001.movePoint(11,0,App.Vector(-0.107088,4.967296,0),0)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Tangent',-3,11))
Gui.getDocument("Pignon" + Type).getObject("Sketch001").Visibility=False
elif 'GT5' == Type:
# On dessine une dent GT5
App.activeDocument().Body.newObject('Sketcher::SketchObject','Sketch001')
App.activeDocument().Sketch001.Support = (App.ActiveDocument.Pad,["Face3"])
App.activeDocument().Sketch001.MapMode = 'FlatFace'
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch001.addGeometry(Part.LineSegment(App.Vector(-1.135752,12.395073,0),App.Vector(-1.122119,13.022206,0)),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Vertical',0))
App.ActiveDocument.Sketch001.addGeometry(Part.LineSegment(App.Vector(-1.135752,13.022206,0),App.Vector(1.427315,13.144907,0)),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',0,2,1,1))
App.ActiveDocument.Sketch001.addGeometry(Part.LineSegment(App.Vector(1.427315,13.144907,0),App.Vector(1.427315,12.531406,0)),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',1,2,2,1))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Vertical',2))
App.ActiveDocument.Sketch001.addGeometry(Part.LineSegment(App.Vector(1.427315,12.531406,0),App.Vector(0.759282,11.195339,0)),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',2,2,3,1))
App.ActiveDocument.Sketch001.addGeometry(Part.LineSegment(App.Vector(0.759282,11.195339,0),App.Vector(-0.522252,11.099906,0)),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',3,2,4,1))
App.ActiveDocument.Sketch001.addGeometry(Part.LineSegment(App.Vector(-0.522252,11.099906,0),App.Vector(-1.122119,12.081507,0)),False)
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',4,2,5,1))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Coincident',5,2,0,1))
App.ActiveDocument.Sketch001.addExternal("Pad","Edge3")
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('PointOnObject',0,1,-3))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Equal',5,3))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Symmetric',4,2,3,2,-2))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Symmetric',0,2,1,2,-2))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceY',2,2,2,1,0.883285))
App.ActiveDocument.Sketch001.setDatum(12,App.Units.Quantity('1.000000 mm'))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceX',1,1,1,2,2.438514))
App.ActiveDocument.Sketch001.setDatum(13,App.Units.Quantity(str(largeurDent) + ' mm'))
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Angle',3,2,5,1,1.008739))
App.ActiveDocument.Sketch001.setDatum(14,App.Units.Quantity(str(angleDent) + ' deg'))
Gui.getDocument("Pignon" + Type).getObject("Sketch001").Visibility=False
###
# On fait un Pocket de la dent GT2, GT3 ou GT5
App.activeDocument().Body.newObject("PartDesign::Pocket","Pocket")
App.activeDocument().Pocket.Profile = App.activeDocument().Sketch001
App.activeDocument().Pocket.Length = largeur_poulie
App.ActiveDocument.recompute()
App.ActiveDocument.Pocket.Length = largeur_poulie
App.ActiveDocument.Pocket.Length2 = 100.000000
App.ActiveDocument.Pocket.Type = 0
App.ActiveDocument.Pocket.UpToFace = None
App.ActiveDocument.Pocket.Reversed = 0
App.ActiveDocument.Pocket.Midplane = 0
App.ActiveDocument.Pocket.Offset = 0.000000
App.ActiveDocument.recompute()
# On dessine toutes les dents
App.activeDocument().Body.newObject("PartDesign::PolarPattern","PolarPattern")
App.ActiveDocument.recompute()
App.activeDocument().PolarPattern.Originals = [App.activeDocument().Pocket,]
App.activeDocument().PolarPattern.Axis = (App.activeDocument().Sketch001, ["N_Axis"])
App.activeDocument().PolarPattern.Angle = 360
App.activeDocument().PolarPattern.Occurrences = int(nombre_de_dents)
App.ActiveDocument.recompute()
App.activeDocument().Body.Tip = App.activeDocument().PolarPattern
App.ActiveDocument.recompute()
App.ActiveDocument.PolarPattern.Originals = [App.ActiveDocument.Pocket,]
Gui.getDocument("Pignon" + Type).getObject("PolarPattern").Visibility=True
App.ActiveDocument.recompute()
close()
def fin():
close()
def close():
dialog.close()
print('Terminé')
###
# Gestion de la fenetre de dialogues
dialog = None
s1 = None
dialog = QtGui.QDialog()
dialog.resize(180,160) # Dimentionner la boite de dialogue
dialog.setWindowTitle("Poulie GT2") # Titre de la fenetre
la = QtGui.QVBoxLayout(dialog)
la.addWidget(QtGui.QLabel("Type de poulie")) # Affichage Label
t0 = QtGui.QComboBox() # Affichage ComboBox
la.addWidget(t0)
t0.addItems(["GT2", "GT3", "GT5"])
la.addWidget(QtGui.QLabel("Nombre de Dents")) # Affichage Label)
s1 = QtGui.QLineEdit() # Affichage LineEdit
s1.setText("16.0")
s1.setFixedWidth(100)
la.addWidget(s1)
la.addWidget(QtGui.QLabel("Largeur Poulie")) # Affichage Label
s2 = QtGui.QLineEdit() # Affichage LineEdit
s2.setText("8.0")
s2.setFixedWidth(100)
la.addWidget(s2)
# Gestion boutons OK / Cancel
okbox = QtGui.QDialogButtonBox(dialog)
okbox.setOrientation(QtCore.Qt.Horizontal)
okbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
la.addWidget(okbox)
QtCore.QObject.connect(okbox, QtCore.SIGNAL("accepted()"), proceed)
QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected()"), close)
dialog.exec_()
Re: Gestion d'une fenetre et des variables destinées au dessin
J'ai réussi à récupérer le nombre de Sketchs et dans recréer d'autre mais le marquage n'est pas Sketch00x, mais Sketchx
Code: Select all
# on récupére le Nb de Sketchs
myDoc = App.ActiveDocument
objs = [obj for obj in myDoc.Objects if str(obj) == "<Sketcher::SketchObject>"]
#print(str(len(objs)))
a = len(objs)
print(a)
Sk = ("Sketch" + str(a))
print(Sk)
#♀ on crée un nouveau Sketch
myDoc.Body.newObject('Sketcher::SketchObject',Sk)
Re: Gestion d'une fenetre et des variables destinées au dessin
Le code qui permet d'afficher le Sketch numéroté façon FreeCad
Il me reste à l'intégrer dans le code
Code: Select all
# on récupére le Nb de Sketchs
myDoc = App.ActiveDocument
objs = [obj for obj in myDoc.Objects if str(obj) == "<Sketcher::SketchObject>"]
a = len(objs)
#Sk = ("Sketch" + str("%03d" % (a,)))
# ou
Sk = ("Sketch" + str("{:03d}".format(a)))
print(Sk)
# on crée un nouveau Sketch
myDoc.Body.newObject('Sketcher::SketchObject',Sk)
Il me reste à l'intégrer dans le code
Re: Gestion d'une fenetre et des variables destinées au dessin
En fait tu t'embêtes. FC a des automatismes pour ne pas attribuer 2 fois le même nom. Donc il suffit de faire :
Code: Select all
mySketch = myDoc.Body.newObject('Sketcher::SketchObject', 'Sketch')
Du coup c'est important de récupérer le pointeur dans la variable 'mySketch' pour bien l'utiliser ensuite.
Re: Gestion d'une fenetre et des variables destinées au dessin
re blonblon
Je pense que tu devrais référencer autant que possible les éléments précédemment créés dans ton code. Ca évitera pas mal de risques, et ça rend le code plus compréhensible.
Voilà ce que ça donnerait sur base du code que tu as publié (je n'ai pas testé, j'espère que ça marche )
Code: Select all
#-*- coding: utf-8 -*-
#
# Dessin pignon GT2
#
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
__DBG__ = False #True #Mettre à False pour supprimer la sortie de debug
def pdbg(text):
if __DBG__: #Si on est en mode debug
App.Console.PrintMessage(text)
def proceed():
###
# On teste le type de poulie
Type = t0.currentText()
###
# Données nécessaires au dessin poulie GT2
if 'GT2' == Type:
pdbg('Type = ' + str(Type)+"\n")
nombre_de_dents = float(s1.text())
pdbg('Nb dents = ' + str(nombre_de_dents)+"\n")
pas = 2
pdbg('Pas = ' + str(pas)+"\n")
PLD = 0.254
pdbg('PLD = ' + str(PLD)+"\n")
h = 0.75
pdbg('h = ' + str(h)+"\n")
circonference = nombre_de_dents * pas
pdbg('circonference = ' + str(circonference)+"\n")
diametre_primitif = circonference / math.pi
pdbg('diametre_primitif = ' + str(diametre_primitif)+"\n")
diametre_poulie = diametre_primitif - (PLD * 2)
pdbg('diametre_poulie = ' + str(diametre_poulie)+"\n")
rayon_poulie = diametre_poulie / 2
pdbg('rayon_poulie = ' + str(rayon_poulie)+"\n")
largeur_poulie = float(s2.text())
pdbg('largeur_poulie = ' + str(largeur_poulie)+"\n")
R1 = 0.15
pdbg('rayon_R1 = ' + str(R1)+"\n")
R2 = 1
pdbg('rayon_R2 = ' + str(R2)+"\n")
R3 = 0.555
pdbg('rayon_R3 = ' + str(R3)+"\n")
b = 0.4
b2 = b*2
elif 'GT3' == Type:
pdbg('Type = ' + str(Type)+"\n")
nombre_de_dents = float(s1.text())
pdbg('Nb dents = ' + str(nombre_de_dents)+"\n")
pas = 3
pdbg('Pas = ' + str(pas)+"\n")
PLD = 0.381
pdbg('PLD = ' + str(PLD)+"\n")
h = 1.14
pdbg('h = ' + str(h)+"\n")
circonference = nombre_de_dents * pas
pdbg('circonference = ' + str(circonference)+"\n")
diametre_primitif = circonference / math.pi
pdbg('diametre_primitif = ' + str(diametre_primitif)+"\n")
diametre_poulie = diametre_primitif - (PLD * 2)
pdbg('diametre_poulie = ' + str(diametre_poulie)+"\n")
rayon_poulie = diametre_poulie / 2
pdbg('rayon_poulie = ' + str(rayon_poulie)+"\n")
largeur_poulie = float(s2.text())
pdbg('largeur_poulie = ' + str(largeur_poulie)+"\n")
R1 = 0.25
pdbg('rayon_R1 = ' + str(R1)+"\n")
R2 = 1.52
pdbg('rayon_R2 = ' + str(R2)+"\n")
R3 = 0.85
pdbg('rayon_R3 = ' + str(R3)+"\n")
b = 0.61
b2 = b*2
elif 'GT5' == Type:
pdbg('Type = ' + str(Type)+"\n")
nombre_de_dents = float(s1.text())
pdbg('Nb dents = ' + str(nombre_de_dents)+"\n")
pas = 5
PLD = 0.43
pdbg('PLD = ' + str(PLD)+"\n")
h = 1.2
pdbg('h = ' + str(h)+"\n")
circonference = nombre_de_dents * pas
pdbg('circonference = ' + str(circonference)+"\n")
diametre_primitif = circonference / math.pi
pdbg('diametre_primitif = ' + str(diametre_primitif)+"\n")
diametre_poulie = diametre_primitif - (PLD * 2)
pdbg('diametre_poulie = ' + str(diametre_poulie)+"\n")
rayon_poulie = diametre_poulie / 2
pdbg('rayon_poulie = ' + str(rayon_poulie)+"\n")
largeur_poulie = float(s2.text())
pdbg('largeur_poulie = ' + str(largeur_poulie)+"\n")
largeurDent = 2.65
angleDent = 40
###
# 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" + Type)
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 len(objs) == 0: #Il n'y a pas de Body on le crée
myBody = myDoc.addObject('PartDesign::Body','Body')
myBody.Label = "Corp"
myPart.addObject(myBody)
else: #Il y a (au moins) un Body dans le Part
myBody = objs[0] #On utilise par ex. le premier Body dans la suite
objs = [obj for obj in myDoc.Objects if str(obj) == "<Sketcher::SketchObject>"]
if len(objs) == 0: #Il n'y a pas de Sketch on le crée
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch.Support = (myBody.Origin.OriginFeatures[3], [''])
mySketch.MapMode = 'FlatFace'
else: #Il y a (au moins) un Sketch dans le Body
mySketch = objs[0] #On utilise par ex. le premier Sketch dans la suite
###
# 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'))
# On extrude le cercle
myPad = myBody.newObject("PartDesign::Pad","Pad")
myPad.Profile = mySketch
myPad.Length = largeur_poulie
myPad.Length = largeur_poulie
myPad.Length2 = 100.000000
myPad.Type = 0
myPad.UpToFace = None
myPad.Reversed = 0
myPad.Midplane = 0
myPad.Offset = 0.000000
myDoc.recompute()
mySketch.Visibility=False
if 'GT2' == Type or 'GT3' == Type:
# On dessine une dent GT2 ou GT3
my2ndSketch = myBody.newObject('Sketcher::SketchObject','Sketch')
my2ndSketch.Support = (myPad,["Face3"])
my2ndSketch.MapMode = 'FlatFace'
myDoc.recompute()
my2ndSketch.addGeometry(Part.LineSegment(App.Vector(-0.193479,5.124362,0),App.Vector(0.192029,5.124362,0)),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Symmetric',0,1,0,2,-2))
my2ndSketch.toggleConstruction(0)
my2ndSketch.addConstraint(Sketcher.Constraint('DistanceX',0,1,0,2,b2)) # Entraxe rayon R2
my2ndSketch.setDatum(1,App.Units.Quantity(str(b2) + ' mm')) # Entraxe rayon R2
geoList = []
geoList.append(Part.LineSegment(App.Vector(-0.733822,4.858931,0),App.Vector(0.675494,4.858931,0)))
geoList.append(Part.LineSegment(App.Vector(0.675494,4.858931,0),App.Vector(0.675494,4.707256,0)))
geoList.append(Part.LineSegment(App.Vector(0.675494,4.707256,0),App.Vector(-0.733822,4.707256,0)))
geoList.append(Part.LineSegment(App.Vector(-0.733822,4.707256,0),App.Vector(-0.733822,4.858931,0)))
my2ndSketch.addGeometry(geoList,False)
conList = []
conList.append(Sketcher.Constraint('Coincident',1,2,2,1))
conList.append(Sketcher.Constraint('Coincident',2,2,3,1))
conList.append(Sketcher.Constraint('Coincident',3,2,4,1))
conList.append(Sketcher.Constraint('Coincident',4,2,1,1))
conList.append(Sketcher.Constraint('Horizontal',1))
conList.append(Sketcher.Constraint('Vertical',2))
conList.append(Sketcher.Constraint('Vertical',4))
my2ndSketch.addConstraint(conList)
my2ndSketch.toggleConstruction(4)
my2ndSketch.toggleConstruction(1)
my2ndSketch.toggleConstruction(3)
my2ndSketch.toggleConstruction(2)
my2ndSketch.addConstraint(Sketcher.Constraint('DistanceY',3,2,1,1,R1)) # Arc cercle R1
my2ndSketch.setDatum(9,App.Units.Quantity(str(R1) + ' mm')) # Arc cercle R1
my2ndSketch.addConstraint(Sketcher.Constraint('Symmetric',3,2,2,2,-2))
my2ndSketch.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(-0.661711,4.752790,0),App.Vector(0,0,1),0.192858),0.236851,1.887364),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',3,2,5,3))
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',1,1,5,2))
my2ndSketch.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.450575,5.100380,0),App.Vector(0,0,1),1.036934),-2.747505,-2.448713),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',0,2,6,3))
my2ndSketch.addConstraint(Sketcher.Constraint('Tangent',5,1,6,1))
my2ndSketch.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.068227,4.563197,0),App.Vector(0,0,1),0.468263),-2.587285,-1.242243),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Tangent',6,2,7,1))
my2ndSketch.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.668608,4.825469,0),App.Vector(0,0,1),0.204199),1.190289,2.657114),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',8,3,2,2))
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',1,2,8,1))
my2ndSketch.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(-0.465797,5.119339,0),App.Vector(0,0,1),1.025216),-0.712926,-0.365625),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',9,3,0,1))
my2ndSketch.addConstraint(Sketcher.Constraint('Tangent',8,2,9,2))
my2ndSketch.addConstraint(Sketcher.Constraint('Tangent',7,2,9,1))
my2ndSketch.addConstraint(Sketcher.Constraint('Radius',6,R2)) # Arc cercle R2
my2ndSketch.setDatum(21,App.Units.Quantity(str(R2) + ' mm')) # Arc cercle R2
my2ndSketch.addConstraint(Sketcher.Constraint('Radius',7,R3)) # Arc cercle R3
my2ndSketch.setDatum(22,App.Units.Quantity(str(R3) + ' mm')) # Arc cercle R3
my2ndSketch.addGeometry(Part.Point(App.Vector(-0.150355,4.112466,0)))
my2ndSketch.addConstraint(Sketcher.Constraint('PointOnObject',10,1,-2))
my2ndSketch.addConstraint(Sketcher.Constraint('PointOnObject',10,1,7))
my2ndSketch.addConstraint(Sketcher.Constraint('DistanceY',10,1,8,1,h)) # Hauteur
my2ndSketch.setDatum(25,App.Units.Quantity(str(h) + ' mm'))
my2ndSketch.addExternal("Pad","Edge3")
my2ndSketch.addConstraint(Sketcher.Constraint('PointOnObject',5,2,-3))
my2ndSketch.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.298317,3.331532,0),App.Vector(0,0,1),1.930821),1.288327,2.078132),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',5,2,11,2))
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',8,1,11,1))
my2ndSketch.movePoint(11,0,App.Vector(-0.107088,4.967296,0),0)
my2ndSketch.addConstraint(Sketcher.Constraint('Tangent',-3,11))
Gui.getDocument("Pignon" + Type).getObject("Sketch001").Visibility=False
elif 'GT5' == Type:
# On dessine une dent GT5
my2ndSketch = myBody.newObject('Sketcher::SketchObject','Sketch001')
my2ndSketch.Support = (myPad,["Face3"])
my2ndSketch.MapMode = 'FlatFace'
myDoc.recompute()
my2ndSketch.addGeometry(Part.LineSegment(App.Vector(-1.135752,12.395073,0),App.Vector(-1.122119,13.022206,0)),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Vertical',0))
my2ndSketch.addGeometry(Part.LineSegment(App.Vector(-1.135752,13.022206,0),App.Vector(1.427315,13.144907,0)),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',0,2,1,1))
my2ndSketch.addGeometry(Part.LineSegment(App.Vector(1.427315,13.144907,0),App.Vector(1.427315,12.531406,0)),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',1,2,2,1))
my2ndSketch.addConstraint(Sketcher.Constraint('Vertical',2))
my2ndSketch.addGeometry(Part.LineSegment(App.Vector(1.427315,12.531406,0),App.Vector(0.759282,11.195339,0)),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',2,2,3,1))
my2ndSketch.addGeometry(Part.LineSegment(App.Vector(0.759282,11.195339,0),App.Vector(-0.522252,11.099906,0)),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',3,2,4,1))
my2ndSketch.addGeometry(Part.LineSegment(App.Vector(-0.522252,11.099906,0),App.Vector(-1.122119,12.081507,0)),False)
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',4,2,5,1))
my2ndSketch.addConstraint(Sketcher.Constraint('Coincident',5,2,0,1))
my2ndSketch.addExternal("Pad","Edge3")
my2ndSketch.addConstraint(Sketcher.Constraint('PointOnObject',0,1,-3))
my2ndSketch.addConstraint(Sketcher.Constraint('Equal',5,3))
my2ndSketch.addConstraint(Sketcher.Constraint('Symmetric',4,2,3,2,-2))
my2ndSketch.addConstraint(Sketcher.Constraint('Symmetric',0,2,1,2,-2))
my2ndSketch.addConstraint(Sketcher.Constraint('DistanceY',2,2,2,1,0.883285))
my2ndSketch.setDatum(12,App.Units.Quantity('1.000000 mm'))
my2ndSketch.addConstraint(Sketcher.Constraint('DistanceX',1,1,1,2,2.438514))
my2ndSketch.setDatum(13,App.Units.Quantity(str(largeurDent) + ' mm'))
my2ndSketch.addConstraint(Sketcher.Constraint('Angle',3,2,5,1,1.008739))
my2ndSketch.setDatum(14,App.Units.Quantity(str(angleDent) + ' deg'))
my2ndSketch.Visibility=False
###
# On fait un Pocket de la dent GT2, GT3 ou GT5
myPocket = myBody.newObject("PartDesign::Pocket","Pocket")
myPocket.Profile = my2ndSketch
myPocket.Length = largeur_poulie
myDoc.recompute()
myPocket.Length = largeur_poulie
myPocket.Length2 = 100.000000
myPocket.Type = 0
myPocket.UpToFace = None
myPocket.Reversed = 0
myPocket.Midplane = 0
myPocket.Offset = 0.000000
myDoc.recompute()
# On dessine toutes les dents
myPattern = myBody.newObject("PartDesign::PolarPattern","PolarPattern")
myDoc.recompute()
myPattern.Originals = [myPocket,]
myPattern.Axis = (my2ndSketch, ["N_Axis"])
myPattern.Angle = 360
myPattern.Occurrences = int(nombre_de_dents)
myDoc.recompute()
myBody.Tip = myPattern
myDoc.recompute()
myPattern.Originals = [myPocket,]
myPattern.Visibility=True
myDoc.recompute()
close()
def fin():
close()
def close():
dialog.close()
print('Terminé')
###
# Gestion de la fenetre de dialogues
dialog = None
s1 = None
dialog = QtGui.QDialog()
dialog.resize(180,160) # Dimentionner la boite de dialogue
dialog.setWindowTitle("Poulie GT2") # Titre de la fenetre
la = QtGui.QVBoxLayout(dialog)
la.addWidget(QtGui.QLabel("Type de poulie")) # Affichage Label
t0 = QtGui.QComboBox() # Affichage ComboBox
la.addWidget(t0)
t0.addItems(["GT2", "GT3", "GT5"])
la.addWidget(QtGui.QLabel("Nombre de Dents")) # Affichage Label)
s1 = QtGui.QLineEdit() # Affichage LineEdit
s1.setText("16.0")
s1.setFixedWidth(100)
la.addWidget(s1)
la.addWidget(QtGui.QLabel("Largeur Poulie")) # Affichage Label
s2 = QtGui.QLineEdit() # Affichage LineEdit
s2.setText("8.0")
s2.setFixedWidth(100)
la.addWidget(s2)
# Gestion boutons OK / Cancel
okbox = QtGui.QDialogButtonBox(dialog)
okbox.setOrientation(QtCore.Qt.Horizontal)
okbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
la.addWidget(okbox)
QtCore.QObject.connect(okbox, QtCore.SIGNAL("accepted()"), proceed)
QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected()"), close)
dialog.exec_()
Re: Gestion d'une fenetre et des variables destinées au dessin
Merci openBrain pour le temps consacré à m'aider.
J'ai regardé ton code mais je ne l'ai pas testé, je le ferais ce soir.
Je modifié la partie affichage afin de pouvoir dessiner des pignons, soit dans un seul Body, soit dans des Body différents, pour l'instant j'ai deux morceaux de codes différents.
Il reste à modifier la fenetre de dialogue pour donner le choix, et voir comment tester en fonction du choix la partie de code qui va etre utilisé.
premier code.
second code
J'ai encore un peu de travail en perspective.
J'ai regardé ton code mais je ne l'ai pas testé, je le ferais ce soir.
Je modifié la partie affichage afin de pouvoir dessiner des pignons, soit dans un seul Body, soit dans des Body différents, pour l'instant j'ai deux morceaux de codes différents.
Il reste à modifier la fenetre de dialogue pour donner le choix, et voir comment tester en fonction du choix la partie de code qui va etre utilisé.
premier code.
Code: Select all
#
# Macro pour ouvrir un Body ecrire les Sketch pour une poulie
# puis on recommance pour une seconde pouie
#
# on teste la presence d'un document
myDoc = App.ActiveDocument
if myDoc == None:
myDoc = App.newDocument("Pignon GT2")
# on teste la presence d'un atelier Part
objs = [obj for obj in myDoc.Objects if str(obj) == "<Part object>"]
if len(objs) == 0: #Il n'y a pas de Part dans le document. On en crée un pour l'utiliser par la suite
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 len(objs) == 0: #Il n'y a pas de Body dans le Part. On en crée un pour l'utiliser par la suite
myBody = myDoc.addObject('PartDesign::Body','Body')
print('Body 0')
else: #Il y a (au moins) un Body dans le Part
myBody = myDoc.addObject('PartDesign::Body','Body')
print('Body 1')
objs = [obj for obj in myDoc.Objects if str(obj) == "<Sketcher::SketchObject>"]
if len(objs) == 0: #Il n'y a pas de Sketch dans le Body
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch.Support = (App.activeDocument().XY_Plane001, [''])
mySketch.MapMode = 'FlatFace'
mySketch1 = mySketch
print('- 0 -')
else: #Il y a (au moins) un Sketch dans le Body
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch.Support = (App.activeDocument().XY_Plane001, [''])
mySketch.MapMode = 'FlatFace'
print('- 1 -')
pass
mySketch.addGeometry(Part.Circle(App.Vector(-0.959298,0.986822,0),App.Vector(0,0,1),12.5),False)
mySketch.addConstraint(Sketcher.Constraint('Coincident',-1,1,0,3))
mySketch.addConstraint(Sketcher.Constraint('Radius',0,12.5))
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1.addGeometry(Part.Circle(App.Vector(-0.959298,0.986822,0),App.Vector(0,0,1),15),False)
mySketch1.addConstraint(Sketcher.Constraint('Coincident',-1,1,0,3))
mySketch1.addConstraint(Sketcher.Constraint('Radius',0,15))
Code: Select all
#
# Macro pour ecrire les Sketch les un apres les autres dans un seul Body
#
# on teste la presence d'un document
myDoc = App.ActiveDocument
if myDoc == None:
myDoc = App.newDocument("Pignon GT2")
# on teste la presence d'un atelier Part
objs = [obj for obj in myDoc.Objects if str(obj) == "<Part object>"]
if len(objs) == 0: #Il n'y a pas de Part dans le document. On en crée un pour l'utiliser par la suite
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 len(objs) == 0: #Il n'y a pas de Body dans le Part. On en crée un pour l'utiliser par la suite
myBody = myDoc.addObject('PartDesign::Body','Body')
print('Body 0')
else: #Il y a (au moins) un Body dans le Part
# myBody = myDoc.addObject('PartDesign::Body','Body')
print('Body 1')
objs = [obj for obj in myDoc.Objects if str(obj) == "<Sketcher::SketchObject>"]
if len(objs) == 0: #Il n'y a pas de Sketch dans le Body
# mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch = myDoc.Body.newObject('Sketcher::SketchObject','Sketch')
mySketch.Support = (App.activeDocument().XY_Plane001, [''])
mySketch.MapMode = 'FlatFace'
mySketch1 = mySketch
print('- 0 -')
else: #Il y a (au moins) un Sketch dans le Body
# mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch = myDoc.Body.newObject('Sketcher::SketchObject','Sketch')
mySketch.Support = (App.activeDocument().XY_Plane001, [''])
mySketch.MapMode = 'FlatFace'
# mySketch1 = mySketch
print('- 1 -')
pass
mySketch.addGeometry(Part.Circle(App.Vector(-0.959298,0.986822,0),App.Vector(0,0,1),12.5),False)
mySketch.addConstraint(Sketcher.Constraint('Coincident',-1,1,0,3))
mySketch.addConstraint(Sketcher.Constraint('Radius',0,12.5))
#mySketch1 = myBody.Body.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myDoc.Body.newObject('Sketcher::SketchObject','Sketch')
mySketch1.addGeometry(Part.Circle(App.Vector(-0.959298,0.986822,0),App.Vector(0,0,1),15),False)
mySketch1.addConstraint(Sketcher.Constraint('Coincident',-1,1,0,3))
mySketch1.addConstraint(Sketcher.Constraint('Radius',0,15))
Re: Gestion d'une fenetre et des variables destinées au dessin
J'ai je pense réussi à faire ce que je voulais.
J'ai un doute sur cette facon d'ecrire le code.
Voici le code de gestion de la checkbox.
La checbox peut etre modifiée au cour du dessin.
J'ai un doute sur cette facon d'ecrire le code.
Code: Select all
obj1 = [obj for obj in myDoc.Objects if str(obj) == "<body object>"]
a = (len(obj1)) - 1
print(str(a))
myBody = objs[int(a)]
Code: Select all
import Part
# t1 simule la case a cocher d'un checbox
t1 = 0 #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')
else:
myBody = myDoc.addObject('PartDesign::Body','Body')
if t1 == 1:
if len(objs) == 0:
myBody = myDoc.addObject('PartDesign::Body','Body')
else:
obj1 = [obj for obj in myDoc.Objects if str(obj) == "<body object>"]
a = (len(obj1)) - 1
print(str(a))
myBody = objs[int(a)]
print('erreur 3')
############
objs = [obj for obj in myDoc.Objects if str(obj) == "<Sketcher::SketchObject>"]
if t1 == 0:
if len(objs) == 0:
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
else:
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
if t1 == 1:
if len(objs) == 0:
mySketch = myBody.newObject('Sketcher::SketchObject','Sketch')
mySketch1 = myBody.newObject('Sketcher::SketchObject','Sketch')
else:
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'))
############
Re: Gestion d'une fenetre et des variables destinées au dessin
Je commence par ça, je regarderai le reste du code un peu plus tard
Python est gentil, et tu peux faire ça directement en 1 ligne :
Note que (comme ton code), ça générera une exception si aucun Body n'est présent (car la liste sera vide).
Dans l'autre snippet que tu as posté, je vois pas mal de cas où tu exécutes la même chose dans le "if" et dans le "else" associé, ce qui n'a pas beaucoup de sens. Ceci dit ça marche, et ce sera éventuellement plus facile à faire évoluer.
Aussi, plutôt que d'écrire "if t1 == 0 : ... if t1 == 1 : ...", tu peux faire "if t1 == 0 : ... elif t1 == 1 : ...". Ce sera plus optimisé en terme d'exécution, et un peu plus propre.
Ce serait plus facile de comprendre avec des commentaires, mais j'imagine que tu cherches à récupérer le dernier Body du document.blonblon wrote: ↑Wed Jul 03, 2019 2:35 pm J'ai un doute sur cette facon d'ecrire le code.Code: Select all
obj1 = [obj for obj in myDoc.Objects if str(obj) == "<body object>"] a = (len(obj1)) - 1 print(str(a)) myBody = objs[int(a)]
Python est gentil, et tu peux faire ça directement en 1 ligne :
Code: Select all
myBody = [obj for obj in myDoc.Objects if str(obj) == "<body object>"][-1]
Dans l'autre snippet que tu as posté, je vois pas mal de cas où tu exécutes la même chose dans le "if" et dans le "else" associé, ce qui n'a pas beaucoup de sens. Ceci dit ça marche, et ce sera éventuellement plus facile à faire évoluer.
Aussi, plutôt que d'écrire "if t1 == 0 : ... if t1 == 1 : ...", tu peux faire "if t1 == 0 : ... elif t1 == 1 : ...". Ce sera plus optimisé en terme d'exécution, et un peu plus propre.
Re: Gestion d'une fenetre et des variables destinées au dessin
J'ai regardé un peu plus le code, et pas grand chose à dire de plus que déjà dit juste avant.
Eventuellement juste qu'un type "case à cocher" à l'avantage d'être booléen True/False.
Ce qui te permettrait d'écrire "if t1 : ... else : ..."
Eventuellement juste qu'un type "case à cocher" à l'avantage d'être booléen True/False.
Ce qui te permettrait d'écrire "if t1 : ... else : ..."