diff --git a/src/partcad/assembly.py b/src/partcad/assembly.py index 4d506cb7..fd029ba2 100644 --- a/src/partcad/assembly.py +++ b/src/partcad/assembly.py @@ -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 @@ -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: @@ -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) diff --git a/src/partcad/assembly_factory.py b/src/partcad/assembly_factory.py index baa0a30e..9be4dac9 100644 --- a/src/partcad/assembly_factory.py +++ b/src/partcad/assembly_factory.py @@ -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 + ) diff --git a/src/partcad/assembly_factory_assy.py b/src/partcad/assembly_factory_assy.py index b32b83d2..943e167c 100644 --- a/src/partcad/assembly_factory_assy.py +++ b/src/partcad/assembly_factory_assy.py @@ -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: @@ -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: @@ -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