-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathExporterOptions.py
More file actions
104 lines (89 loc) · 3.81 KB
/
Copy pathExporterOptions.py
File metadata and controls
104 lines (89 loc) · 3.81 KB
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
"""
Container for all options pertaining to the Fusion Exporter.
These options are saved per-design and are passed to the parser upon design export.
"""
import json
import os
import platform
from dataclasses import dataclass, field, fields
from typing import Any, List
import adsk.core
from adsk.fusion import CalculationAccuracy, TriangleMeshQualityOptions
from src import INTERNAL_ID
from src.Logging import getLogger, logFailure, timed
from src.Types import (
KG,
ExportLocation,
ExportMode,
Gamepiece,
Joint,
ModelHierarchy,
PhysicalDepth,
Wheel,
encodeNestedObjects,
makeObjectFromJson,
)
@dataclass
class ExporterOptions:
# Python's `os` module can return `None` when attempting to find the home directory if the
# user's computer has conflicting configs of some sort. This has happened and should be accounted
# for accordingly.
fileLocation: str | os.PathLike[str] | None = field(
default=(os.getenv("HOME") if platform.system() == "Windows" else os.path.expanduser("~"))
)
name: str | None = field(default=None)
version: str | None = field(default=None)
materials: int = field(default=0)
exportMode: ExportMode = field(default=ExportMode.ROBOT)
wheels: List[Wheel] = field(default_factory=list)
joints: list[Joint] = field(default_factory=list)
gamepieces: list[Gamepiece] = field(default_factory=list)
robotWeight: KG = field(default=KG(0.0))
autoCalcRobotWeight: bool = field(default=False)
autoCalcGamepieceWeight: bool = field(default=False)
frictionOverride: bool = field(default=False)
frictionOverrideCoeff: float = field(default=0.5)
compressOutput: bool = field(default=True)
exportAsPart: bool = field(default=False)
exportLocation: ExportLocation = field(default=ExportLocation.DOWNLOAD)
openSynthesisUponExport: bool = field(default=False)
hierarchy: ModelHierarchy = field(default=ModelHierarchy.FusionAssembly)
visualQuality: TriangleMeshQualityOptions = field(default=TriangleMeshQualityOptions.LowQualityTriangleMesh)
physicalDepth: PhysicalDepth = field(default=PhysicalDepth.AllOccurrence)
physicalCalculationLevel: CalculationAccuracy = field(default=CalculationAccuracy.LowCalculationAccuracy)
@logFailure
@timed
def readFromDesign(self) -> "ExporterOptions":
designAttributes = adsk.core.Application.get().activeProduct.attributes
for field in fields(self):
attribute = designAttributes.itemByName(INTERNAL_ID, field.name)
if attribute:
attrJsonData = makeObjectFromJson(field.type, json.loads(attribute.value))
setattr(self, field.name, attrJsonData)
self.visualQuality = TriangleMeshQualityOptions.LowQualityTriangleMesh
return self
@logFailure
# @timed
def readFromJSON(self, data: dict[str, Any]) -> "ExporterOptions":
for field in fields(self):
attribute = data.get(field.name)
if attribute is not None:
attrJsonData = makeObjectFromJson(field.type, attribute)
setattr(self, field.name, attrJsonData)
self.visualQuality = TriangleMeshQualityOptions.LowQualityTriangleMesh
return self
@logFailure
@timed
def writeToDesign(self) -> None:
designAttributes = adsk.core.Application.get().activeProduct.attributes
for field in fields(self):
data = json.dumps(getattr(self, field.name), default=encodeNestedObjects, indent=4)
designAttributes.add(INTERNAL_ID, field.name, data)
@logFailure
@timed
def writeToJson(self) -> dict[str, Any]:
out = {}
for field in fields(self):
data = json.dumps(getattr(self, field.name), default=encodeNestedObjects, indent=4)
out[field.name] = json.loads(data)
return out