forked from Zolko-123/FreeCAD_Assembly4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newSketchCmd.py
89 lines (67 loc) · 2.67 KB
/
newSketchCmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
# coding: utf-8
#
# newSketchCmd.py
import math, re, os
from PySide import QtGui, QtCore
import FreeCADGui as Gui
import FreeCAD as App
import Part
import libAsm4 as Asm4
class newSketch:
"My tool object"
def GetResources(self):
return {"MenuText": "New Sketch",
"ToolTip": "Create a new Sketch in a Part",
"Pixmap" : os.path.join( Asm4.iconPath , 'Asm4_Sketch.svg')
}
def IsActive(self):
if App.ActiveDocument:
# is something selected ?
if Gui.Selection.getSelection():
# This command adds a new Sketch only to App::Part objects ...
if Gui.Selection.getSelection()[0].TypeId == ('App::Part'):
return(True)
else:
return(False)
# ... or if there is a Model object in the active document:
elif App.ActiveDocument.getObject('Model'):
return(True)
#
else:
return(False)
else:
return(False)
def checkPart(self):
# if something is selected ...
if Gui.Selection.getSelection():
selectedObj = Gui.Selection.getSelection()[0]
# ... and it's an App::Part:
if selectedObj.TypeId == 'App::Part':
return(selectedObj)
# or of nothing is selected ...
if App.ActiveDocument.getObject('Model'):
# ... but there is a Model:
return App.ActiveDocument.getObject('Model')
else:
return(False)
"""
+-----------------------------------------------+
| the real stuff |
+-----------------------------------------------+
"""
def Activated(self):
# check that we have somewhere to put our stuff
partChecked = self.checkPart()
sketchName = 'Sketch_1'
if partChecked:
# input dialog to ask the user the name of the Sketch:
text,ok = QtGui.QInputDialog.getText(None,'Create new Sketch','Enter Sketch name : ', text = sketchName)
if ok and text:
# App.activeDocument().getObject('Model').newObject( 'Sketcher::SketchObject', text )
createdSketch = partChecked.newObject( 'Sketcher::SketchObject', text )
Gui.Selection.clearSelection()
Gui.Selection.addSelection( App.ActiveDocument.Name, partChecked.Name, createdSketch.Name+'.' )
Gui.runCommand('Part_EditAttachment',0)
# add the command to the workbench
Gui.addCommand( 'Asm4_newSketch', newSketch() )