You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.9 KiB
58 lines
1.9 KiB
import os |
|
|
|
import FreeCADGui as Gui |
|
import FreeCAD as App |
|
|
|
class AHB_ExportCsv: |
|
def GetResources(self): |
|
return {"MenuText": "CSV export", |
|
"ToolTip": "Export part list as a CSV file", |
|
"Pixmap": "" |
|
} |
|
|
|
def IsActive(self): |
|
return True |
|
|
|
def Activated(self): |
|
doc = App.activeDocument() |
|
|
|
fileName: str = doc.FileName |
|
if fileName is None: |
|
raise BaseException("You must save your FreeCAD document before exporting to CSV") |
|
|
|
csvFileName = os.path.splitext(fileName)[0] + ".csv" |
|
|
|
print("Exporting parts to CSV file " + csvFileName + "...") |
|
|
|
def readProp(obj, prop, default): |
|
if prop in obj.PropertiesList: |
|
return obj.getPropertyByName(prop) |
|
else: |
|
return default |
|
|
|
parts = [] |
|
obj: App.DocumentObject |
|
for obj in doc.Objects: |
|
if obj.TypeId == 'Part::Feature': |
|
parts.append((readProp(obj, 'Base_Layer', '').replace(',', '-') + "," + readProp(obj, 'Base_Reference', '').replace(',', '-'), "=\"" + obj.Label.replace(',', '-') + "\"")) |
|
|
|
parts.sort() |
|
|
|
with open(csvFileName, 'w', encoding='utf8') as csvFile: |
|
csvFile.write("Group, Reference, Name, Quantity\n") |
|
|
|
quantity = 0 |
|
prevPart = parts[0] |
|
for partIdx in range(len(parts) + 1): |
|
part = parts[partIdx] if partIdx < len(parts) else ("eof", "eof") |
|
if part[0] == prevPart[0]: |
|
quantity += 1 |
|
else: |
|
csvFile.write(prevPart[0] + "," + prevPart[1] + "," + str(quantity) + "\n") |
|
quantity = 1 |
|
prevPart = part |
|
|
|
print("CSV file exported") |
|
|
|
from ahb_command import AHB_CommandWrapper |
|
AHB_CommandWrapper.addGuiCommand('AHB_exportCsv', AHB_ExportCsv())
|
|
|