Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load assemblies #40

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/partcad/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ def __init__(self, name=None, config={}):
# TODO(clairbee): add reference counter to assemblies
self.count = 0

self.instantiated = False

def do_instantiate(self):
if not self.instantiated:
self.instantiate(self)
self.instantiated = True

def add(
self,
child_item: shape.Shape, # pc.Part or pc.Assembly
name=None,
loc=b3d.Location((0.0, 0.0, 0.0), (0.0, 0.0, 1.0), 0.0),
):
# It's called from instantiate() only, so no need to do_instantiate()
self.children.append(AssemblyChild(child_item, name, loc))

# Keep part reference counter for bill-of-materials purposes
Expand All @@ -60,10 +68,12 @@ def add(
self.shape = None

def ref_inc(self):
self.do_instantiate()
for child in self.children:
child.item.ref_inc()

def get_shape(self):
self.do_instantiate()
if self.shape is None:
child_shapes = []
for child in self.children:
Expand All @@ -82,9 +92,11 @@ def get_shape(self):
return copy.copy(self.shape)

def _render_txt_real(self, file):
self.do_instantiate()
for child in self.children:
child._render_txt_real(file)

def _render_markdown_real(self, file):
self.do_instantiate()
for child in self.children:
child._render_markdown_real(file)
6 changes: 4 additions & 2 deletions src/partcad/assembly_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def __init__(self, ctx, project, assembly_config, extension=""):

def _create(self, assembly_config):
self.assembly = assembly.Assembly(self.name, assembly_config)

def _save(self):
self.project.assemblies[self.name] = self.assembly

self.assembly.instantiate = lambda assembly_self: self.instantiate(
assembly_self
)
6 changes: 3 additions & 3 deletions src/partcad/assembly_factory_assy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, ctx, project, assembly_config):
# Complement the config object here if necessary
self._create(assembly_config)

def instantiate(self, assembly):
self.assy = {}
if os.path.exists(self.path):
try:
Expand All @@ -35,9 +36,7 @@ def __init__(self, ctx, project, assembly_config):
logging.error("ERROR: Assembly file not found: %s" % self.path)

if "links" in self.assy and not self.assy["links"] is None:
self.handle_node_list(self.assembly, self.assy["links"])

self._save()
self.handle_node_list(assembly, self.assy["links"])

def handle_node_list(self, assembly, node_list):
for link in node_list:
Expand All @@ -62,6 +61,7 @@ def handle_node(self, assembly, node):
# Check if this node is for an assembly
if "links" in node:
item = Assembly(name, node["links"])
item.instantiate = lambda x: True
self.handle_node_list(item, node["links"])
else:
# This is a node for a part
Expand Down