forked from youen/assembly_handbook
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.
75 lines
2.7 KiB
75 lines
2.7 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 in ['Part::Feature', 'App::Part']: |
|
if obj.TypeId == 'App::Part' and ('Base_Reference' not in obj.PropertiesList or obj.Base_Reference == ''): |
|
continue |
|
|
|
parent = obj.Parents[0][0] if len(obj.Parents) > 0 else None |
|
if parent is not None and parent.TypeId != 'App::Part': |
|
parent = None |
|
|
|
if parent is not None and 'Base_Reference' in parent.PropertiesList and parent.Base_Reference != "": |
|
continue |
|
|
|
parts.append((readProp(obj, 'Base_Layer', '').replace(',', '-') + "," + readProp(obj, 'Base_Reference', '').replace(',', '-'), obj.Label.replace(',', '-'))) |
|
|
|
parts.sort() |
|
|
|
export_layer_name = False |
|
with open(csvFileName, 'w', encoding='utf8') as csvFile: |
|
if export_layer_name: |
|
csvFile.write("Group,") |
|
csvFile.write("Reference, First object 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: |
|
if export_layer_name: |
|
csvFile.write(prevPart[0] + ",") |
|
else: |
|
csvFile.write(prevPart[0].split(',')[1] + ",") |
|
csvFile.write(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())
|
|
|