Skip to content

Commit

Permalink
Support macros and console logs in Assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
bgbsww committed Sep 21, 2024
1 parent 86fa65b commit 9f3f24a
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 37 deletions.
12 changes: 12 additions & 0 deletions src/App/DocumentPy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ For a temporary document it returns its transient directory.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="getUniqueObjectName">
<Documentation>
<UserDocu>getUniqueObjectName(objName) -> objName

Return the same name, or the name made unique, for Example Box -> Box002 if there are conflicting name
already in the document.

ObjName : str
Object name.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="mergeProject">
<Documentation>
<UserDocu>Merges this document with another project file</UserDocu>
Expand Down
12 changes: 12 additions & 0 deletions src/App/DocumentPyImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ PyObject* DocumentPy::getFileName(PyObject* args)
return Py::new_reference_to(Py::String(fn));
}

PyObject* DocumentPy::getUniqueObjectName(PyObject *args)
{
char *sName;
if (!PyArg_ParseTuple(args, "s", &sName))
return nullptr;
PY_TRY {
auto newName = getDocumentPtr()->getUniqueObjectName(sName);
return Py::new_reference_to(Py::String(newName));
}
PY_CATCH;
}

PyObject* DocumentPy::mergeProject(PyObject * args)
{
char* filename;
Expand Down
2 changes: 1 addition & 1 deletion src/App/GeoFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ DocumentObject *GeoFeature::resolveElement(DocumentObject *obj, const char *subn
}
if(geoFeature)
*geoFeature = geo;
if(!obj || (filter && geo!=filter))
if(filter && geo!=filter)
return nullptr;
if(!element || !element[0]) {
if(append)
Expand Down
4 changes: 2 additions & 2 deletions src/Gui/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <map>
#include <string>

#define putpix()

#include <App/Application.h>

class QCloseEvent;
Expand Down Expand Up @@ -340,6 +338,8 @@ class GuiExport Application

static PyObject* sDoCommand (PyObject *self,PyObject *args);
static PyObject* sDoCommandGui (PyObject *self,PyObject *args);
static PyObject* sDoCommandEval (PyObject *self,PyObject *args);
static PyObject* sDoCommandSkip (PyObject *self,PyObject *args);
static PyObject* sAddModule (PyObject *self,PyObject *args);

static PyObject* sShowDownloads (PyObject *self,PyObject *args);
Expand Down
50 changes: 50 additions & 0 deletions src/Gui/ApplicationPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,19 @@ PyMethodDef Application::Methods[] = {
"but doesn't record it in macros.\n"
"\n"
"cmd : str"},
{"doCommandEval", (PyCFunction) Application::sDoCommandEval, METH_VARARGS,
"doCommandEval(cmd) -> PyObject\n"
"\n"
"Runs the given string without showing in the python console or recording in\n"
"macros, and returns the result.\n"
"\n"
"cmd : str"},
{"doCommandSkip", (PyCFunction) Application::sDoCommandSkip, METH_VARARGS,
"doCommandSkip(cmd) -> None\n"
"\n"
"Record the given string in the Macro but comment it out in the console\n"
"\n"
"cmd : str"},
{"addModule", (PyCFunction) Application::sAddModule, METH_VARARGS,
"addModule(mod) -> None\n"
"\n"
Expand Down Expand Up @@ -1352,6 +1365,43 @@ PyObject* Application::sDoCommandGui(PyObject * /*self*/, PyObject *args)
return PyRun_String(sCmd, Py_file_input, dict, dict);
}

PyObject* Application::sDoCommandEval(PyObject * /*self*/, PyObject *args)
{
char *sCmd = nullptr;
if (!PyArg_ParseTuple(args, "s", &sCmd))
return nullptr;

Gui::Command::LogDisabler d1;
Gui::SelectionLogDisabler d2;

PyObject *module, *dict;

Base::PyGILStateLocker locker;
module = PyImport_AddModule("__main__");
if (!module)
return nullptr;

dict = PyModule_GetDict(module);
if (!dict)
return nullptr;

return PyRun_String(sCmd, Py_eval_input, dict, dict);
}

PyObject* Application::sDoCommandSkip(PyObject * /*self*/, PyObject *args)
{
char *sCmd = nullptr;
if (!PyArg_ParseTuple(args, "s", &sCmd))
return nullptr;

Gui::Command::LogDisabler d1;
Gui::SelectionLogDisabler d2;

Gui::Command::printPyCaller();
Gui::Application::Instance->macroManager()->addLine(MacroManager::App, sCmd);
return Py::None().ptr();
}

PyObject* Application::sAddModule(PyObject * /*self*/, PyObject *args)
{
char *pstr;
Expand Down
19 changes: 14 additions & 5 deletions src/Mod/Assembly/CommandCreateAssembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,24 @@ def Activated(self):
App.setActiveTransaction("Create assembly")

activeAssembly = UtilsAssembly.activeAssembly()
Gui.addModule("UtilsAssembly")
if activeAssembly:
assembly = activeAssembly.newObject("Assembly::AssemblyObject", "Assembly")
commands = (
"activeAssembly = UtilsAssembly.activeAssembly()\n"
'assembly = activeAssembly.newObject("Assembly::AssemblyObject", "Assembly")\n'
)
else:
assembly = App.ActiveDocument.addObject("Assembly::AssemblyObject", "Assembly")
commands = (
'assembly = App.ActiveDocument.addObject("Assembly::AssemblyObject", "Assembly")\n'
)

assembly.Type = "Assembly"
commands = commands + 'assembly.Type = "Assembly"\n'
commands = commands + 'assembly.newObject("Assembly::JointGroup", "Joints")'

Gui.doCommand(commands)
if not activeAssembly:
Gui.ActiveDocument.setEdit(assembly)
assembly.newObject("Assembly::JointGroup", "Joints")
Gui.doCommandGui("Gui.ActiveDocument.setEdit(assembly)")

App.closeActiveTransaction()


Expand Down
11 changes: 8 additions & 3 deletions src/Mod/Assembly/CommandCreateBom.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,16 @@ def isNameDuplicate(self, name):

def createBomObject(self):
assembly = UtilsAssembly.activeAssembly()
Gui.addModule("UtilsAssembly")
if assembly is not None:
bom_group = UtilsAssembly.getBomGroup(assembly)
self.bomObj = bom_group.newObject("Assembly::BomObject", "Bill of Materials")
commands = (
"bom_group = UtilsAssembly.getBomGroup(assembly)\n"
'bomObj = bom_group.newObject("Assembly::BomObject", "Bill of Materials")'
)
else:
self.bomObj = App.activeDocument().addObject("Assembly::BomObject", "Bill of Materials")
commands = 'bomObj = App.activeDocument().addObject("Assembly::BomObject", "Bill of Materials")'
Gui.doCommand(commands)
self.bomObj = Gui.doCommandEval("bomObj")

def export(self):
self.bomObj.recompute()
Expand Down
36 changes: 23 additions & 13 deletions src/Mod/Assembly/CommandCreateJoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ def activateJoint(index):
if JointObject.activeTask:
JointObject.activeTask.reject()

panel = TaskAssemblyCreateJoint(index)
dialog = Gui.Control.showDialog(panel)
Gui.addModule("JointObject") # NOLINT

Check failure on line 61 in src/Mod/Assembly/CommandCreateJoint.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Possibly using variable 'Gui' before assignment (possibly-used-before-assignment)
Gui.doCommand(f"panel = JointObject.TaskAssemblyCreateJoint({index})")
Gui.doCommandGui("dialog = Gui.Control.showDialog(panel)")
dialog = Gui.doCommandEval("dialog")
if dialog is not None:
dialog.setAutoCloseOnTransactionChange(True)
dialog.setDocumentName(App.ActiveDocument.Name)
Expand Down Expand Up @@ -476,16 +478,21 @@ def IsActive(self):


def createGroundedJoint(obj):
assembly = UtilsAssembly.activeAssembly()
if not assembly:
if not UtilsAssembly.activeAssembly():
return

joint_group = UtilsAssembly.getJointGroup(assembly)

ground = joint_group.newObject("App::FeaturePython", "GroundedJoint")
JointObject.GroundedJoint(ground, obj)
JointObject.ViewProviderGroundedJoint(ground.ViewObject)
return ground
Gui.addModule("UtilsAssembly")
Gui.addModule("JointObject")
commands = (
f'obj = App.ActiveDocument.getObject("{obj.Name}")\n'
"assembly = UtilsAssembly.activeAssembly()\n"
"joint_group = UtilsAssembly.getJointGroup(assembly)\n"
'ground = joint_group.newObject("App::FeaturePython", "GroundedJoint")\n'
"JointObject.GroundedJoint(ground, obj)"
)
Gui.doCommand(commands)
Gui.doCommandGui("JointObject.ViewProviderGroundedJoint(ground.ViewObject)")
return Gui.doCommandEval("ground")


class CommandToggleGrounded:
Expand Down Expand Up @@ -540,9 +547,12 @@ def Activated(self):
ungrounded = False
for joint in joint_group.Group:
if hasattr(joint, "ObjectToGround") and joint.ObjectToGround == moving_part:
doc = App.ActiveDocument
doc.removeObject(joint.Name)
doc.recompute()
commands = (
"doc = App.ActiveDocument\n"
f'doc.removeObject("{joint.Name}")\n'
"doc.recompute()\n"
)
Gui.doCommand(commands)
ungrounded = True
break
if ungrounded:
Expand Down
38 changes: 28 additions & 10 deletions src/Mod/Assembly/CommandCreateView.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ def Activated(self):
if not assembly:
return

self.panel = TaskAssemblyCreateView()
Gui.Control.showDialog(self.panel)
Gui.addModule("CommandCreateView") # NOLINT

Check failure on line 77 in src/Mod/Assembly/CommandCreateView.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Possibly using variable 'Gui' before assignment (possibly-used-before-assignment)
Gui.doCommand("panel = CommandCreateView.TaskAssemblyCreateView()")
self.panel = Gui.doCommandEval("panel")
Gui.doCommandGui("Gui.Control.showDialog(panel)")


######### Exploded View Object ###########
Expand Down Expand Up @@ -524,6 +526,11 @@ def accept(self):
UtilsAssembly.restoreAssemblyPartsPlacements(self.assembly, self.initialPlcs)
for move in self.viewObj.Moves:
move.Visibility = False
commands = f'obj = App.ActiveDocument.getObject("{self.viewObj.Name}")\n'
for move in self.viewObj.Moves:
more = UtilsAssembly.generatePropertySettings("obj.Moves[0]", move)
commands = commands + more
Gui.doCommand(commands[:-1]) # Don't use the last \n
App.closeActiveTransaction()
return True

Expand Down Expand Up @@ -695,16 +702,27 @@ def setDraggerObjectPlc(self):
self.blockDraggerMove = False

def createExplodedViewObject(self):
view_group = UtilsAssembly.getViewGroup(self.assembly)
self.viewObj = view_group.newObject("App::FeaturePython", "Exploded View")

ExplodedView(self.viewObj)
ViewProviderExplodedView(self.viewObj.ViewObject)
Gui.addModule("UtilsAssembly")
commands = (
f'assembly = App.ActiveDocument.getObject("{self.assembly.Name}")\n'
"view_group = UtilsAssembly.getViewGroup(assembly)\n"
'viewObj = view_group.newObject("App::FeaturePython", "Exploded View")\n'
"CommandCreateView.ExplodedView(viewObj)"
)
Gui.doCommand(commands)
self.viewObj = Gui.doCommandEval("viewObj")
Gui.doCommandGui("CommandCreateView.ViewProviderExplodedView(viewObj.ViewObject)")

def createExplodedStepObject(self, moveType_index=0):
self.currentStep = self.assembly.newObject("App::FeaturePython", "Move")
ExplodedViewStep(self.currentStep, moveType_index)
ViewProviderExplodedViewStep(self.currentStep.ViewObject)
commands = (
f'assembly = App.ActiveDocument.getObject("{self.assembly.Name}")\n'
'currentStep = assembly.newObject("App::FeaturePython", "Move")\n'
f"CommandCreateView.ExplodedViewStep(currentStep, {moveType_index})"
)
Gui.doCommand(commands)
self.currentStep = Gui.doCommandEval("currentStep")
Gui.doCommandGui("CommandCreateView.ViewProviderExplodedViewStep(currentStep.ViewObject)")

self.currentStep.MovementTransform = App.Placement()

Expand All @@ -727,7 +745,7 @@ def dismissCurrentStep(self):
for obj, init_plc in zip(self.selectedObjs, self.selectedObjsInitPlc):
obj.Placement = init_plc

self.currentStep.Document.removeObject(self.currentStep.Name)
Gui.doCommand(f'App.ActiveDocument.removeObject("{self.currentStep.Name}")')
self.currentStep = None

Gui.Selection.clearSelection()
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Assembly/CommandExportASMT.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def Activated(self):
)

if filePath:
assembly.exportAsASMT(filePath)
Gui.doCommand(f'assembly.exportAsASMT("{filePath}")')

Check failure on line 77 in src/Mod/Assembly/CommandExportASMT.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Possibly using variable 'Gui' before assignment (possibly-used-before-assignment)


if App.GuiUp:
Expand Down
26 changes: 25 additions & 1 deletion src/Mod/Assembly/CommandInsertLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def Activated(self):
if not assembly:
return
view = Gui.activeDocument().activeView()

Check failure on line 82 in src/Mod/Assembly/CommandInsertLink.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Possibly using variable 'Gui' before assignment (possibly-used-before-assignment)

self.panel = TaskAssemblyInsertLink(assembly, view)
Gui.Control.showDialog(self.panel)

Expand Down Expand Up @@ -125,7 +124,32 @@ def accept(self):

# if self.partMoving:
# self.endMove()
Gui.addModule("UtilsAssembly")
commands = "assembly = UtilsAssembly.activeAssembly()\n"
for insertionItem in self.insertionStack:
object = insertionItem["addedObject"]
translation = insertionItem["translation"]
commands = commands + (
f'item = assembly.newObject("App::Link", "{object.Name}")\n'
f'item.LinkedObject = App.ActiveDocument.getObject("{object.LinkedObject.Name}")\n'
f'item.Label = "{object.Label}"\n'
)

if translation != App.Vector():
commands = commands + (
f"item.Placement.base = App.Vector({translation.x}."
f"{translation.y},"
f"{translation.z})\n"
)

# Ground the first item if that happened
if self.groundedObj:
commands = (
commands
+ f'CommandCreateJoint.createGroundedJoint(App.ActiveDocument.getObject("{self.groundedObj.Name}"))\n'
)

Gui.doCommandSkip(commands[:-1]) # Get rid of last \n
App.closeActiveTransaction()
return True

Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Assembly/CommandSolveAssembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def Activated(self):
if not assembly:
return

Gui.addModule("UtilsAssembly")
App.setActiveTransaction("Solve assembly")
assembly.solve()
Gui.doCommand("UtilsAssembly.activeAssembly().solve()")
App.closeActiveTransaction()


Expand Down
3 changes: 3 additions & 0 deletions src/Mod/Assembly/JointObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,9 @@ def accept(self):
else:
self.joint.Document.removeObject(self.joint.Name)

cmds = UtilsAssembly.generatePropertySettings("obj", self.joint)
Gui.doCommand(cmds)

App.closeActiveTransaction()
return True

Expand Down
Loading

0 comments on commit 9f3f24a

Please sign in to comment.