Scripting PartDesign Workbench: Difference between revisions
mNo edit summary |
(Trying to insert code) |
||
Line 5: | Line 5: | ||
Here we will explain to you how to control the [[File:Workbench_PartDesign.svg|24px]] [[PartDesign_Workbench|PartDesign]] using a script. Be sure to browse the [[Scripting]] section and the [[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]] pages if you need more information about how Python scripting works in FreeCAD. If you are new to Python, it is a good idea to first read the [[Introduction_to_Python|Introduction to Python]]. |
Here we will explain to you how to control the [[File:Workbench_PartDesign.svg|24px]] [[PartDesign_Workbench|PartDesign]] using a script. Be sure to browse the [[Scripting]] section and the [[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]] pages if you need more information about how Python scripting works in FreeCAD. If you are new to Python, it is a good idea to first read the [[Introduction_to_Python|Introduction to Python]]. |
||
{{Code|code= |
|||
"""Script to replicate Part Design Tutorial Example. |
|||
https://wiki.freecadweb.org/Basic_Part_Design_Tutorial |
|||
This code was written as an sample code |
|||
Portion of code courtesy of edwilliams16 |
|||
Name: 20221011_pdtut_ew2.py |
|||
Author: Carlo Dormeletti |
|||
Copyright: 2022 |
|||
Licence: CC BY-NC-ND 4.0 |
|||
""" |
|||
import os |
|||
import FreeCAD # noqa |
|||
import FreeCADGui # noqa |
|||
import Sketcher |
|||
import Part |
|||
from FreeCAD import Placement, Rotation, Vector # noqa |
|||
def activate_doc(doc_name): |
|||
"""Activate a specific document.""" |
|||
FreeCAD.setActiveDocument(doc_name) |
|||
FreeCAD.ActiveDocument = FreeCAD.getDocument(doc_name) |
|||
FreeCADGui.ActiveDocument = FreeCADGui.getDocument(doc_name) |
|||
print(f"Document: {doc_name} activated") |
|||
def clear_doc(doc_name): |
|||
"""Clear the document deleting all the objects.""" |
|||
doc = FreeCAD.getDocument(doc_name) |
|||
for obj in doc.Objects: |
|||
try: |
|||
doc.removeObject(obj.Name) |
|||
except Exception: |
|||
pass |
|||
def setview(doc): |
|||
"""Rearrange View.""" |
|||
try: |
|||
doc_v = FreeCAD.Gui.activeView() |
|||
FreeCAD.Gui.SendMsgToActiveView("ViewFit") |
|||
doc_v.viewAxometric() |
|||
except Exception: |
|||
pass |
|||
def check_exist(doc_name): |
|||
"""Check the existence of a FC document named doc_name. |
|||
If it not exist create one. |
|||
""" |
|||
try: |
|||
doc = FreeCAD.getDocument(doc_name) |
|||
except NameError: |
|||
doc = FreeCAD.newDocument(doc_name) |
|||
return doc |
|||
# CODE start here |
|||
# Some handy abbreviations |
|||
VEC0 = Vector(0, 0, 0) |
|||
ROT0 = Rotation(0, 0, 0) |
|||
}} |
|||
===See also=== |
===See also=== |
Revision as of 08:13, 2 March 2023
Introduction
Here we will explain to you how to control the PartDesign using a script. Be sure to browse the Scripting section and the FreeCAD Scripting Basics pages if you need more information about how Python scripting works in FreeCAD. If you are new to Python, it is a good idea to first read the Introduction to Python.
"""Script to replicate Part Design Tutorial Example.
https://wiki.freecadweb.org/Basic_Part_Design_Tutorial
This code was written as an sample code
Portion of code courtesy of edwilliams16
Name: 20221011_pdtut_ew2.py
Author: Carlo Dormeletti
Copyright: 2022
Licence: CC BY-NC-ND 4.0
"""
import os
import FreeCAD # noqa
import FreeCADGui # noqa
import Sketcher
import Part
from FreeCAD import Placement, Rotation, Vector # noqa
def activate_doc(doc_name):
"""Activate a specific document."""
FreeCAD.setActiveDocument(doc_name)
FreeCAD.ActiveDocument = FreeCAD.getDocument(doc_name)
FreeCADGui.ActiveDocument = FreeCADGui.getDocument(doc_name)
print(f"Document: {doc_name} activated")
def clear_doc(doc_name):
"""Clear the document deleting all the objects."""
doc = FreeCAD.getDocument(doc_name)
for obj in doc.Objects:
try:
doc.removeObject(obj.Name)
except Exception:
pass
def setview(doc):
"""Rearrange View."""
try:
doc_v = FreeCAD.Gui.activeView()
FreeCAD.Gui.SendMsgToActiveView("ViewFit")
doc_v.viewAxometric()
except Exception:
pass
def check_exist(doc_name):
"""Check the existence of a FC document named doc_name.
If it not exist create one.
"""
try:
doc = FreeCAD.getDocument(doc_name)
except NameError:
doc = FreeCAD.newDocument(doc_name)
return doc
# CODE start here
# Some handy abbreviations
VEC0 = Vector(0, 0, 0)
ROT0 = Rotation(0, 0, 0)