Skip to content

Commit 0bb5619

Browse files
committed
format code with ruff, refactor dialog input handling
1 parent 7aca699 commit 0bb5619

11 files changed

Lines changed: 814 additions & 714 deletions

File tree

commands/commandDialog/entry.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33
from ...lib import fusionAddInUtils as futil
44
from ... import config
55
from ...lib.panelUtils.panel_command import command_created, CMD_NAME, CMD_Description
6+
67
app = adsk.core.Application.get()
78
ui = app.userInterface
89

910

1011
# TODO *** Specify the command identity information. ***
11-
CMD_ID = f'{config.COMPANY_NAME}_{config.ADDIN_NAME}_cmdDialog'
12+
CMD_ID = f"{config.COMPANY_NAME}_{config.ADDIN_NAME}_cmdDialog"
1213

1314
# Specify that the command will be promoted to the panel.
1415
IS_PROMOTED = True
1516

1617
# TODO *** Define the location where the command button will be created. ***
17-
# This is done by specifying the workspace, the tab, and the panel, and the
18+
# This is done by specifying the workspace, the tab, and the panel, and the
1819
# command it will be inserted beside. Not providing the command to position it
1920
# will insert it at the end.
20-
WORKSPACE_ID = 'FusionSolidEnvironment'
21-
PANEL_ID = 'SolidCreatePanel'
22-
COMMAND_BESIDE_ID = 'ScriptsManagerCommand'
21+
WORKSPACE_ID = "FusionSolidEnvironment"
22+
PANEL_ID = "SolidCreatePanel"
23+
COMMAND_BESIDE_ID = "ScriptsManagerCommand"
2324

2425
# Resource location for command icons, here we assume a sub folder in this directory named "resources".
25-
ICON_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', '')
26+
ICON_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", "")
2627

2728

2829
# Executed when add-in is run.
@@ -43,7 +44,7 @@ def start():
4344
# Create the button command control in the UI after the specified existing command.
4445
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)
4546

46-
# Specify if the command is promoted to the main toolbar.
47+
# Specify if the command is promoted to the main toolbar.
4748
control.isPromoted = IS_PROMOTED
4849

4950

lib/generalUtils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
from .sketch_utils import *
33
from .extrude_utils import *
44
from .persist_utils import *
5-
from .debug_utils import *
5+
from .debug_utils import *

lib/generalUtils/debug_utils.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import adsk.core, adsk.fusion
1+
from enum import Enum
2+
import adsk.core
3+
import adsk.fusion
24
from ..fusionAddInUtils import log
35

46
app = adsk.core.Application.get()
57
ui = app.userInterface
68

9+
710
def alert(msg):
8-
ui.messageBox(str(msg))
11+
ui.messageBox(str(msg))
12+
913

1014
def identifyFaces(body: adsk.fusion.BRepBody):
11-
component = body.parentComponent
12-
sketches = component.sketches
13-
for i, face in enumerate(body.faces):
14-
try:
15-
sketch = sketches.add(face)
16-
sketch.name = 'Face {}'.format(i)
17-
except Exception as err:
18-
log(f'Error occurred, {err}')
15+
component = body.parentComponent
16+
sketches = component.sketches
17+
for i, face in enumerate(body.faces):
18+
try:
19+
sketch = sketches.add(face)
20+
sketch.name = "Face {}".format(i)
21+
except Exception as err:
22+
log(f"Error occurred, {err}")

lib/generalUtils/extrude_utils.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import adsk.core, adsk.fusion
1+
import adsk.core
2+
import adsk.fusion
23
from .value_utils import getNormalizedValueInput
34
from typing import TypedDict, NotRequired, Unpack
45

@@ -7,35 +8,37 @@
78

89
app = adsk.core.Application.get()
910

11+
1012
class ExtrudeKwargs(TypedDict):
11-
operation: NotRequired[adsk.fusion.FeatureOperations]
12-
offsetFrom: NotRequired[adsk.core.Base]
13+
operation: NotRequired[adsk.fusion.FeatureOperations]
14+
offsetFrom: NotRequired[adsk.core.Base]
15+
1316

1417
def extrude(
15-
component: adsk.fusion.Component,
16-
sketch: adsk.fusion.Sketch,
17-
profileIndices: list[int],
18-
height: float,
19-
name: str,
20-
**kwargs: Unpack[ExtrudeKwargs]
18+
component: adsk.fusion.Component,
19+
sketch: adsk.fusion.Sketch,
20+
profileIndices: list[int],
21+
height: float,
22+
name: str,
23+
**kwargs: Unpack[ExtrudeKwargs],
2124
):
22-
operation = kwargs.get('operation', adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
23-
offsetFrom = kwargs.get('offsetFrom')
24-
25-
profiles = adsk.core.ObjectCollection.create()
26-
for i in profileIndices:
27-
profiles.add(sketch.profiles.item(i))
28-
29-
features = component.features
30-
extrudeFeatures = features.extrudeFeatures
31-
32-
extrudeInput = extrudeFeatures.createInput(profiles, operation) # type: ignore
33-
extent = adsk.fusion.DistanceExtentDefinition.create(getNormalizedValueInput(height))
34-
extrudeInput.setOneSideExtent(extent, adsk.fusion.ExtentDirections.PositiveExtentDirection) # type: ignore
35-
if offsetFrom:
36-
extrudeInput.startExtent = adsk.fusion.FromEntityStartDefinition.create(offsetFrom, adsk.core.ValueInput.createByReal(0))
37-
38-
extrude = extrudeFeatures.add(extrudeInput)
39-
extrude.name = "Extrude {}".format(name)
40-
body = extrude.bodies.item(0)
41-
return body
25+
operation = kwargs.get("operation", adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
26+
offsetFrom = kwargs.get("offsetFrom")
27+
28+
profiles = adsk.core.ObjectCollection.create()
29+
for i in profileIndices:
30+
profiles.add(sketch.profiles.item(i))
31+
32+
features = component.features
33+
extrudeFeatures = features.extrudeFeatures
34+
35+
extrudeInput = extrudeFeatures.createInput(profiles, operation) # type: ignore
36+
extent = adsk.fusion.DistanceExtentDefinition.create(getNormalizedValueInput(height))
37+
extrudeInput.setOneSideExtent(extent, adsk.fusion.ExtentDirections.PositiveExtentDirection) # type: ignore
38+
if offsetFrom:
39+
extrudeInput.startExtent = adsk.fusion.FromEntityStartDefinition.create(offsetFrom, adsk.core.ValueInput.createByReal(0))
40+
41+
extrude = extrudeFeatures.add(extrudeInput)
42+
extrude.name = "Extrude {}".format(name)
43+
body = extrude.bodies.item(0)
44+
return body

lib/generalUtils/persist_utils.py

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,64 @@
33
import json
44
from ...lib import fusionAddInUtils as futil
55

6-
persistDir = normpath(join(dirname(abspath(__file__)), '../../persist_defaults'))
6+
persistDir = normpath(join(dirname(abspath(__file__)), "../../persist_defaults"))
77

8-
class Persistable():
9-
def __init__(self, persistFile: str, defaults: dict):
10-
self.persistFile = join(persistDir, persistFile)
11-
self._defaults = defaults
12-
self.restoreDefaults()
13-
14-
def saveDefaults(self):
15-
try:
16-
if not exists(persistDir):
17-
mkdir(persistDir)
18-
with open(self.persistFile, 'w') as persistFile:
19-
data = {}
20-
for key in self._defaults.keys():
21-
data[key] = getattr(self, key)
22-
json.dump(data, persistFile, indent=2)
23-
futil.log(f'saved defaults file {self.persistFile}')
24-
return True
25-
except:
26-
futil.log(f'error when attempting to save defaults file {self.persistFile}')
27-
return False
288

29-
def __loadDefaults(self):
30-
try:
31-
data = False
32-
if exists(self.persistFile):
33-
with open(self.persistFile) as persistFile:
34-
data = json.load(persistFile)
35-
futil.log(f'loaded defaults file {self.persistFile}')
36-
else:
37-
futil.log(f'no defaults file to load {self.persistFile}')
38-
return data
39-
except:
40-
futil.log(f'error when attempting to load defaults file {self.persistFile}')
41-
return False
9+
class Persistable:
10+
def __init__(self, persistFile: str, defaults: dict):
11+
self.persistFile = join(persistDir, persistFile)
12+
self._defaults = defaults
13+
self.restoreDefaults()
4214

43-
def restoreDefaults(self):
44-
data = self._defaults | (self.__loadDefaults() or {})
45-
for key, value in data.items():
46-
setattr(self, key, value)
15+
def saveDefaults(self):
16+
try:
17+
if not exists(persistDir):
18+
mkdir(persistDir)
19+
with open(self.persistFile, "w") as persistFile:
20+
data = {}
21+
for key in self._defaults.keys():
22+
data[key] = getattr(self, key)
23+
json.dump(data, persistFile, indent=4)
24+
futil.log(f"saved defaults file {self.persistFile}")
25+
return True
26+
except Exception as err:
27+
futil.log(f"error when attempting to save defaults file {self.persistFile}: {err}")
28+
return False
4729

48-
# Ensure invalid values loaded from persistFile don't break things
49-
def ensureDefaultKeyIsValid(self, keyName, obj):
50-
key = getattr(self, keyName)
51-
if not key in obj:
52-
futil.log(f'Warning: {keyName} "{key}" invalid, restoring default value "{self._defaults[keyName]}"')
53-
setattr(self, keyName, self._defaults[keyName])
30+
def __loadDefaults(self):
31+
try:
32+
data = False
33+
if exists(self.persistFile):
34+
with open(self.persistFile) as persistFile:
35+
data = json.load(persistFile)
36+
futil.log(f"loaded defaults file {self.persistFile}")
37+
else:
38+
futil.log(f"no defaults file to load {self.persistFile}")
39+
return data
40+
except Exception as err:
41+
futil.log(f"error when attempting to load defaults file {self.persistFile}: {err}")
42+
return False
5443

55-
def eraseDefaults(self):
56-
try:
57-
if exists(self.persistFile):
58-
remove(self.persistFile)
59-
futil.log(f'erased defaults file {self.persistFile}')
60-
else:
61-
futil.log(f'no defaults file to erase {self.persistFile}')
62-
return True
63-
except:
64-
futil.log(f'error when attempting to erase defaults file {self.persistFile}')
65-
return False
44+
def restoreDefaults(self):
45+
data = self._defaults | (self.__loadDefaults() or {})
46+
for key, value in data.items():
47+
setattr(self, key, value)
6648

49+
# Ensure invalid values loaded from persistFile don't break things
50+
def ensureDefaultKeyIsValid(self, keyName, obj):
51+
key = getattr(self, keyName)
52+
if key not in obj:
53+
futil.log(f'Warning: {keyName} "{key}" invalid, restoring default value "{self._defaults[keyName]}"')
54+
setattr(self, keyName, self._defaults[keyName])
55+
56+
def eraseDefaults(self):
57+
try:
58+
if exists(self.persistFile):
59+
remove(self.persistFile)
60+
futil.log(f"erased defaults file {self.persistFile}")
61+
else:
62+
futil.log(f"no defaults file to erase {self.persistFile}")
63+
return True
64+
except Exception as err:
65+
futil.log(f"error when attempting to erase defaults file {self.persistFile}: {err}")
66+
return False

0 commit comments

Comments
 (0)