forked from Zolko-123/FreeCAD_Assembly4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewPartCmd.py
115 lines (94 loc) · 3.97 KB
/
newPartCmd.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
# coding: utf-8
#
# LGPL
# Copyright HUBERT Zoltán
#
# newPartCmd.py
import math, re, os
from PySide import QtGui, QtCore
import FreeCADGui as Gui
import FreeCAD as App
import Part
import Asm4_libs as Asm4
"""
+-----------------------------------------------+
| a class to create all container objects |
| App::Part or PartDesign::Body |
+-----------------------------------------------+
"""
class newPart:
def __init__(self, partName):
self.partName = partName
if self.partName == 'Part':
self.partType = 'App::Part'
self.menutext = "New Part"
self.tooltip = "Create a new Part"
self.icon = os.path.join( Asm4.iconPath , 'Asm4_Part.svg')
elif self.partName == 'Body':
self.partType = 'PartDesign::Body'
self.menutext = "New Body"
self.tooltip = "Create a new Body"
self.icon = os.path.join( Asm4.iconPath , 'Asm4_Body.svg')
elif self.partName == 'Group':
self.partType = 'App::DocumentObjectGroup'
self.menutext = "New Group"
self.tooltip = "Create a new Group"
self.icon = os.path.join( Asm4.iconPath , 'Asm4_Group.svg')
def GetResources(self):
return {"MenuText" : self.menutext,
"ToolTip" : self.tooltip,
"Pixmap" : self.icon
}
def IsActive(self):
if App.ActiveDocument:
return(True)
else:
return(False)
def checkPart(self):
selectedPart = None
# if an App::Part is selected
if Gui.Selection.getSelection():
selectedObj = Gui.Selection.getSelection()[0]
if selectedObj.TypeId == 'App::Part':
selectedPart = selectedObj
return selectedPart
def Activated(self):
instanceName = Asm4.nextInstance( self.partName )
text,ok = QtGui.QInputDialog.getText(None, self.tooltip, 'Enter new '+self.partName+' name :'+' '*30, text = instanceName)
if ok and text:
# create Part
newPart = App.ActiveDocument.addObject(self.partType,text)
newPart.Label = text
# add stuff if appropriate (not for groups)
if self.partType in Asm4.containerTypes:
# add an LCS at the root of the Part, and attach it to the 'Origin'
lcs0 = newPart.newObject('PartDesign::CoordinateSystem','LCS_0')
lcs0.Support = [(newPart.Origin.OriginFeatures[0],'')]
lcs0.MapMode = 'ObjectXY'
lcs0.MapReversed = False
# add AttachmentEngine
# oooops, no, creates problems because it creates an AttachmentOffset property that collides with Asm4
# newPart.addExtension("Part::AttachExtensionPython")
# If an App::Part container is selected, move the created part/body/group there
container = None
if len(Gui.Selection.getSelection())==1:
obj = Gui.Selection.getSelection()[0]
if obj.TypeId == 'App::Part':
container = obj
if container :
if newPart.Name != 'Assembly':
container.addObject(newPart)
# If the 'Part' group exists, move it there:
else:
partsGroup = App.ActiveDocument.getObject('Parts')
if partsGroup and partsGroup.TypeId=='App::DocumentObjectGroup' and newPart.TypeId in Asm4.containerTypes:
if newPart.Name != 'Assembly':
partsGroup.addObject(newPart)
# recompute
newPart.recompute()
App.ActiveDocument.recompute()
# add the command to the workbench
Gui.addCommand( 'Asm4_newPart', newPart('Part') )
Gui.addCommand( 'Asm4_newBody', newPart('Body') )
Gui.addCommand( 'Asm4_newGroup', newPart('Group'))