Exemple #1
0
 def addComponent(self, shape, name, color):
     """Add new shape to top assembly of self.doc & return uid"""
     labels = TDF_LabelSequence()
     shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main())
     color_tool = XCAFDoc_DocumentTool_ColorTool(self.doc.Main())
     shape_tool.GetShapes(labels)
     try:
         rootLabel = labels.Value(1)  # First label at root
     except RuntimeError as e:
         print(e)
         return
     newLabel = shape_tool.AddComponent(rootLabel, shape, True)
     # Get referrred label and apply color to it
     refLabel = TDF_Label()  # label of referred shape
     isRef = shape_tool.GetReferredShape(newLabel, refLabel)
     if isRef:
         color_tool.SetColor(refLabel, color, XCAFDoc_ColorGen)
     self.setLabelName(newLabel, name)
     logger.info('Part %s added to root label', name)
     shape_tool.UpdateAssemblies()
     self.doc = self.doc_linter()  # This gets color to work
     self.parse_doc()
     entry = newLabel.EntryDumpToString()
     uid = entry + '.0'  # this should work OK since it is new
     return uid
Exemple #2
0
    def replaceShape(self, uid, modshape):
        """Replace referred shape with modshape of component with uid

        The modified part is a located instance of a referred shape stored
        at doc root. The user doesn't have access to this root shape. In order
        to modify this referred shape, the modified instance shape is moved
        back to the original location at doc root, then saved."""
        shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main())
        color_tool = XCAFDoc_DocumentTool_ColorTool(self.doc.Main())
        # shape is stored at label entry '0:1:1:n'
        n = int(self.label_dict[uid]['ref_entry'].split(':')[-1])
        color = self.part_dict[uid]['color']
        labels = TDF_LabelSequence()
        shape_tool.GetShapes(labels)
        label = labels.Value(n)  # nth label at root

        # If shape instance was moved from its root location to its instance
        # location, 'unmove' it to relocate it back to the root location.
        if self.part_dict[uid]['loc']:
            modshape.Move(self.part_dict[uid]['loc'].Inverted())
        # Replace oldshape in self.doc
        shape_tool.SetShape(label, modshape)
        color_tool.SetColor(modshape, color, XCAFDoc_ColorGen)
        shape_tool.UpdateAssemblies()
        self.parse_doc()  # generate new part_dict
Exemple #3
0
    def saveStep(self):
        """Export self.doc to STEP file."""

        prompt = 'Choose filename for step file.'
        fnametuple = QFileDialog.getSaveFileName(
            None, prompt, './', "STEP files (*.stp *.STP *.step)")
        fname, _ = fnametuple
        if not fname:
            print("Save step cancelled.")
            return
        # Reconstruct XCAFDoc related code from stepXD.StepImporter
        labels = TDF_LabelSequence()
        shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main())
        shape_tool.GetShapes(labels)
        logger.info('Number of labels at root : %i', labels.Length())
        try:
            rootlabel = labels.Value(1)  # First label at root
        except RuntimeError:
            return
        name = rootlabel.GetLabelName()
        logger.info('Name of root label: %s', name)
        isAssy = shape_tool.IsAssembly(rootlabel)
        logger.info("First label at root holds an assembly? %s", isAssy)

        # Modify self.doc by adding active part to rootlabel.
        #Standard_Boolean expand = Standard_False; //default
        #TDF_Label aLabel = myAssembly->AddComponent (aShape [,expand]);
        newLabel = shape_tool.AddComponent(rootlabel, self.activePart, True)
        #set a name to newlabel (as a reminder using OCAF), use:
        #TCollection_ExtendedString aName ...;
        #// contains the desired name for this Label (ASCII)
        #TDataStd_Name::Set (aLabel, aName);
        newName = TCollection_ExtendedString(
            self._nameDict[self.activePartUID])
        TDataStd_Name.Set(newLabel, newName)
        logger.info('Name of new part: %s', newName)
        #myAssembly->UpdateAssemblies();
        shape_tool.UpdateAssemblies()

        # initialize the STEP exporter
        step_writer = STEPCAFControl_Writer()

        # transfer shapes and write file
        step_writer.Transfer(self.doc)
        status = step_writer.Write(fname)
        assert status == IFSelect_RetDone
Exemple #4
0
 def change_label_name(self, uid, name):
     """Change the name of component with uid."""
     entry, _ = uid.split('.')
     entry_parts = entry.split(':')
     if len(entry_parts) == 4:  # first label at root
         j = 1
         k = None
     elif len(entry_parts) == 5:  # part is a component of label at root
         j = int(entry_parts[3])  # number of label at root
         k = int(entry_parts[4])  # component number
     shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main())
     color_tool = XCAFDoc_DocumentTool_ColorTool(self.doc.Main())
     labels = TDF_LabelSequence()  # labels at root of self.doc
     shape_tool.GetShapes(labels)
     label = labels.Value(j)
     comps = TDF_LabelSequence()  # Components of root_label
     subchilds = False
     is_assy = shape_tool.GetComponents(label, comps, subchilds)
     target_label = comps.Value(k)
     self.setLabelName(target_label, name)
     shape_tool.UpdateAssemblies()
     print(f"Name {name} set for part with uid = {uid}.")
     self.parse_doc()
Exemple #5
0
    def load_stp_undr_top(self):
        """Paste step root label under 1st label at self.doc root

        Add a simple component to the first label at self.doc root.
        Set the component name to be the name of the step file.
        Then assign the label of the referred shape to 'targetLabel'.
        Finally, copy step root label onto 'targetLabel'.

        This works when copying file 'as1-oc-214.stp' to 0:1:1:2 (n=2) but does
        not get part color at higher values of n. Also doesn't work with file
        'as1_pe_203.stp' loaded at any value of n. ???
        """

        prompt = 'Select STEP file to import'
        fnametuple = QFileDialog.getOpenFileName(
            None, prompt, './', "STEP files (*.stp *.STP *.step)")
        fname, _ = fnametuple  # fname = /path/to/some/filename.ext
        base = os.path.basename(fname)  # filename.ext
        filename, ext = os.path.splitext(base)
        logger.debug("Load file name: %s", fname)
        if not fname:
            print("Load step cancelled")
            return
        # Get the step data
        tmodel = TreeModel("STEP")
        step_shape_tool = tmodel.shape_tool
        step_color_tool = tmodel.color_tool

        step_reader = STEPCAFControl_Reader()
        step_reader.SetColorMode(True)
        step_reader.SetLayerMode(True)
        step_reader.SetNameMode(True)
        step_reader.SetMatMode(True)

        status = step_reader.ReadFile(fname)
        if status == IFSelect_RetDone:
            logger.info("Transfer doc to STEPCAFControl_Reader")
            step_reader.Transfer(tmodel.doc)
        # Delint tmodel.doc & make new tools
        step_doc = self.doc_linter(tmodel.doc)
        step_shape_tool = XCAFDoc_DocumentTool_ShapeTool(step_doc.Main())
        step_color_tool = XCAFDoc_DocumentTool_ColorTool(step_doc.Main())

        # Get root label of step data
        step_labels = TDF_LabelSequence()
        step_shape_tool.GetShapes(step_labels)
        steprootLabel = step_labels.Value(1)
        # Make a simple box and add it as a component
        myBody = BRepPrimAPI_MakeBox(4, 4, 4).Shape()
        _ = self.addComponent(myBody, filename, Quantity_ColorRGBA())
        step_shape_tool.UpdateAssemblies()
        # Get target label of self.doc
        labels = TDF_LabelSequence()  # labels at root
        shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main())
        color_tool = XCAFDoc_DocumentTool_ColorTool(self.doc.Main())
        shape_tool.GetShapes(labels)
        n = labels.Length()  # number of labels at root
        print(n)
        targetLabel = labels.Value(n)  # of ref shape of comp just added
        # Copy source label to target label
        self.copy_label(steprootLabel, targetLabel)
        shape_tool.UpdateAssemblies()
        # Repair self.doc by cycling through save/load
        self.doc = self.doc_linter()
        # Build new self.part_dict & tree view
        self.parse_doc()