示例#1
0
    def find_components(self, comps):
        """Discover components from comps (LabelSequence) of an assembly.

        Components of an assembly are, by definition, references which refer
        to either a shape or another assembly. Components are essentially
        'instances' of the referred shape or assembly, and carry a location
        vector specifing the location of the referred shape or assembly.
        """
        for j in range(comps.Length()):
            c_label = comps.Value(j + 1)  # component label <class 'TDF_Label'>
            c_name = c_label.GetLabelName()
            c_entry = c_label.EntryDumpToString()
            ref_label = TDF_Label()  # label of referred shape (or assembly)
            is_ref = self.shape_tool.GetReferredShape(c_label, ref_label)
            if is_ref:  # just in case all components are not references
                ref_entry = ref_label.EntryDumpToString()
                ref_name = ref_label.GetLabelName()
                indent = "\t" * self.indent
                self.output += f"{self.uid}{indent}[{c_entry}] {c_name}"
                self.output += f" => [{ref_entry}] {ref_name}\n"
                self.uid += 1
                if self.shape_tool.IsAssembly(ref_label):
                    self.indent += 1
                    ref_comps = TDF_LabelSequence()  # Components of Assy
                    subchilds = False
                    _ = self.shape_tool.GetComponents(ref_label, ref_comps,
                                                      subchilds)
                    if ref_comps.Length():
                        self.find_components(ref_comps)

        self.indent -= 1
示例#2
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
示例#3
0
    def find_subshape(self, label, shape):
        """
        Find a label for the sub-shape stored on the given label.

        :param afem.exchange.xde.XdeLabel label: The label.
        :param afem.topology.entities.Shape shape: The sub-shape.

        :return: The sub-shape label if found, *None* otherwise.
        :rtype: afem.exchange.xde.XdeLabel or None
        """
        sub_label = TDF_Label()
        status, sub_label = self._tool.FindSubShape(label.object, shape.object,
                                                    sub_label)
        if not status:
            return None
        return XdeLabel(sub_label)
示例#4
0
def getSubShapes(lab, loc):
    global cnt, lvl
    cnt += 1
    print("\n[%d] level %d, handling LABEL %s\n" %
          (cnt, lvl, get_label_name(lab)))
    print()
    print(lab.DumpToString())
    print()
    print("Is Assembly    :", shape_tool.IsAssembly(lab))
    print("Is Free        :", shape_tool.IsFree(lab))
    print("Is Shape       :", shape_tool.IsShape(lab))
    print("Is Compound    :", shape_tool.IsCompound(lab))
    print("Is Component   :", shape_tool.IsComponent(lab))
    print("Is SimpleShape :", shape_tool.IsSimpleShape(lab))
    print("Is Reference   :", shape_tool.IsReference(lab))

    users = TDF_LabelSequence()
    users_cnt = shape_tool.GetUsers(lab, users)
    print("Nr Users       :", users_cnt)

    l_subss = TDF_LabelSequence()
    shape_tool.GetSubShapes(lab, l_subss)
    print("Nb subshapes   :", l_subss.Length())
    l_comps = TDF_LabelSequence()
    shape_tool.GetComponents(lab, l_comps)
    print("Nb components  :", l_comps.Length())
    print()

    if shape_tool.IsAssembly(lab):
        l_c = TDF_LabelSequence()
        shape_tool.GetComponents(lab, l_c)
        for i in range(l_c.Length()):
            label = l_c.Value(i + 1)
            if shape_tool.IsReference(label):
                print("\n########  reference label :", label)
                label_reference = TDF_Label()
                shape_tool.GetReferredShape(label, label_reference)
                loc = shape_tool.GetLocation(label)
                print("    loc          :", loc)
                trans = loc.Transformation()
                print("    tran form    :", trans.Form())
                rot = trans.GetRotation()
                print("    rotation     :", rot)
                print("    X            :", rot.X())
                print("    Y            :", rot.Y())
                print("    Z            :", rot.Z())
                print("    W            :", rot.W())
                tran = trans.TranslationPart()
                print("    translation  :", tran)
                print("    X            :", tran.X())
                print("    Y            :", tran.Y())
                print("    Z            :", tran.Z())

                locs.append(loc)
                print(">>>>")
                lvl += 1
                getSubShapes(label_reference, loc)
                lvl -= 1
                print("<<<<")
                locs.pop()

    elif shape_tool.IsSimpleShape(lab):
        print("\n########  simpleshape label :", lab)
        shape = shape_tool.GetShape(lab)
        print("    all ass locs   :", locs)

        loc = TopLoc_Location()
        for i in range(len(locs)):
            print("    take loc       :", locs[i])
            loc = loc.Multiplied(locs[i])

        trans = loc.Transformation()
        print("    FINAL loc    :")
        print("    tran form    :", trans.Form())
        rot = trans.GetRotation()
        print("    rotation     :", rot)
        print("    X            :", rot.X())
        print("    Y            :", rot.Y())
        print("    Z            :", rot.Z())
        print("    W            :", rot.W())
        tran = trans.TranslationPart()
        print("    translation  :", tran)
        print("    X            :", tran.X())
        print("    Y            :", tran.Y())
        print("    Z            :", tran.Z())
        shape = BRepBuilderAPI_Transform(shape, loc.Transformation()).Shape()

        c = Quantity_Color()
        colorSet = False
        if (color_tool.GetInstanceColor(shape, 0, c)
                or color_tool.GetInstanceColor(shape, 1, c)
                or color_tool.GetInstanceColor(shape, 2, c)):
            for i in (0, 1, 2):
                color_tool.SetInstanceColor(shape, i, c)
            colorSet = True
            n = c.Name(c.Red(), c.Green(), c.Blue())
            print('    instance color Name & RGB: ', c, n, c.Red(), c.Green(),
                  c.Blue())

        if not colorSet:
            if (color_tool.GetColor(lab, 0, c)
                    or color_tool.GetColor(lab, 1, c)
                    or color_tool.GetColor(lab, 2, c)):
                for i in (0, 1, 2):
                    color_tool.SetInstanceColor(shape, i, c)

                n = c.Name(c.Red(), c.Green(), c.Blue())
                print('    shape color Name & RGB: ', c, n, c.Red(), c.Green(),
                      c.Blue())

        # n = c.Name(c.Red(), c.Green(), c.Blue())
        # print('    color Name & RGB: ', c, n, c.Red(), c.Green(), c.Blue())
        # Display shape
        display.DisplayColoredShape(shape, c)

        for i in range(l_subss.Length()):
            lab = l_subss.Value(i + 1)
            print("\n########  simpleshape subshape label :", lab)
            shape = shape_tool.GetShape(lab)

            c = Quantity_Color()
            colorSet = False
            if (color_tool.GetInstanceColor(shape, 0, c)
                    or color_tool.GetInstanceColor(shape, 1, c)
                    or color_tool.GetInstanceColor(shape, 2, c)):
                for i in (0, 1, 2):
                    color_tool.SetInstanceColor(shape, i, c)
                colorSet = True
                n = c.Name(c.Red(), c.Green(), c.Blue())
                print('    instance color Name & RGB: ', c, n, c.Red(),
                      c.Green(), c.Blue())

            if not colorSet:
                if (color_tool.GetColor(lab, 0, c)
                        or color_tool.GetColor(lab, 1, c)
                        or color_tool.GetColor(lab, 2, c)):
                    for i in (0, 1, 2):
                        color_tool.SetInstanceColor(shape, i, c)

                    n = c.Name(c.Red(), c.Green(), c.Blue())
                    print('    shape color Name & RGB: ', c, n, c.Red(),
                          c.Green(), c.Blue())

            # n = c.Name(c.Red(), c.Green(), c.Blue())
            # print('    color Name & RGB: ', c, n, c.Red(), c.Green(), c.Blue())
            # Display shape
            display.DisplayColoredShape(shape, c)
示例#5
0
    def findComponents(self, label, comps):
        """Discover components from comps (LabelSequence) of an assembly (label).

        Components of an assembly are, by definition, references which refer to
        either a shape or another assembly. Components are essentially 'instances'
        of the referred shape or assembly, and carry a location vector specifing
        the location of the referred shape or assembly.
        """
        logger.debug("")
        logger.debug("Finding components of label entry %s)", label.EntryDumpToString())
        for j in range(comps.Length()):
            logger.debug("loop %i of %i", j+1, comps.Length())
            cLabel = comps.Value(j+1)  # component label <class 'OCC.Core.TDF.TDF_Label'>
            cShape = self.shape_tool.GetShape(cLabel)
            logger.debug("Component number %i", j+1)
            logger.debug("Component entry: %s", cLabel.EntryDumpToString())
            name = self.getName(cLabel)
            logger.debug("Component name: %s", name)
            refLabel = TDF_Label()  # label of referred shape (or assembly)
            isRef = self.shape_tool.GetReferredShape(cLabel, refLabel)
            if isRef:  # I think all components are references, but just in case...
                refShape = self.shape_tool.GetShape(refLabel)
                refLabelEntry = refLabel.EntryDumpToString()
                logger.debug("Entry referred to: %s", refLabelEntry)
                refName = self.getName(refLabel)
                logger.debug("Name of referred item: %s", refName)
                if self.shape_tool.IsSimpleShape(refLabel):
                    logger.debug("Referred item is a Shape")
                    logger.debug("Name of Shape: %s", refName)
                    tempAssyLocStack = list(self.assyLocStack)
                    tempAssyLocStack.reverse()

                    for loc in tempAssyLocStack:
                        cShape.Move(loc)

                    color = self.getColor(refShape)
                    self.tree.create_node(name,
                                          self.getNewUID(),
                                          self.assyUidStack[-1],
                                          {'a': False, 'l': None, 'c': color, 's': cShape})
                elif self.shape_tool.IsAssembly(refLabel):
                    logger.debug("Referred item is an Assembly")
                    logger.debug("Name of Assembly: %s", refName)
                    name = self.getName(cLabel)  # Instance name
                    aLoc = TopLoc_Location()
                    # Location vector is carried by component
                    aLoc = self.shape_tool.GetLocation(cLabel)
                    self.assyLocStack.append(aLoc)
                    newAssyUID = self.getNewUID()
                    self.tree.create_node(name,
                                          newAssyUID,
                                          self.assyUidStack[-1],
                                          {'a': True, 'l': aLoc, 'c': None, 's': None})
                    self.assyUidStack.append(newAssyUID)
                    rComps = TDF_LabelSequence() # Components of Assy
                    subchilds = False
                    isAssy = self.shape_tool.GetComponents(refLabel, rComps, subchilds)
                    logger.debug("Assy name: %s", name)
                    logger.debug("Is Assembly? %s", isAssy)
                    logger.debug("Number of components: %s", rComps.Length())
                    if rComps.Length():
                        self.findComponents(refLabel, rComps)
        self.assyUidStack.pop()
        self.assyLocStack.pop()
示例#6
0
    def _get_sub_shapes(lab, loc):
        #global cnt, lvl
        #cnt += 1
        #print("\n[%d] level %d, handling LABEL %s\n" % (cnt, lvl, _get_label_name(lab)))
        #print()
        #print(lab.DumpToString())
        #print()
        #print("Is Assembly    :", shape_tool.IsAssembly(lab))
        #print("Is Free        :", shape_tool.IsFree(lab))
        #print("Is Shape       :", shape_tool.IsShape(lab))
        #print("Is Compound    :", shape_tool.IsCompound(lab))
        #print("Is Component   :", shape_tool.IsComponent(lab))
        #print("Is SimpleShape :", shape_tool.IsSimpleShape(lab))
        #print("Is Reference   :", shape_tool.IsReference(lab))

        #users = TDF_LabelSequence()
        #users_cnt = shape_tool.GetUsers(lab, users)
        #print("Nr Users       :", users_cnt)

        l_subss = TDF_LabelSequence()
        shape_tool.GetSubShapes(lab, l_subss)
        #print("Nb subshapes   :", l_subss.Length())
        l_comps = TDF_LabelSequence()
        shape_tool.GetComponents(lab, l_comps)
        #print("Nb components  :", l_comps.Length())
        #print()
        name = lab.GetLabelName()
        print("Name :", name)

        if shape_tool.IsAssembly(lab):
            l_c = TDF_LabelSequence()
            shape_tool.GetComponents(lab, l_c)
            for i in range(l_c.Length()):
                label = l_c.Value(i + 1)
                if shape_tool.IsReference(label):
                    #print("\n########  reference label :", label)
                    label_reference = TDF_Label()
                    shape_tool.GetReferredShape(label, label_reference)
                    loc = shape_tool.GetLocation(label)
                    #print("    loc          :", loc)
                    #trans = loc.Transformation()
                    #print("    tran form    :", trans.Form())
                    #rot = trans.GetRotation()
                    #print("    rotation     :", rot)
                    #print("    X            :", rot.X())
                    #print("    Y            :", rot.Y())
                    #print("    Z            :", rot.Z())
                    #print("    W            :", rot.W())
                    #tran = trans.TranslationPart()
                    #print("    translation  :", tran)
                    #print("    X            :", tran.X())
                    #print("    Y            :", tran.Y())
                    #print("    Z            :", tran.Z())

                    locs.append(loc)
                    #print(">>>>")
                    #lvl += 1
                    _get_sub_shapes(label_reference, loc)
                    #lvl -= 1
                    #print("<<<<")
                    locs.pop()

        elif shape_tool.IsSimpleShape(lab):
            #print("\n########  simpleshape label :", lab)
            shape = shape_tool.GetShape(lab)
            #print("    all ass locs   :", locs)

            loc = TopLoc_Location()
            for l in locs:
                #print("    take loc       :", l)
                loc = loc.Multiplied(l)

            #trans = loc.Transformation()
            #print("    FINAL loc    :")
            #print("    tran form    :", trans.Form())
            #rot = trans.GetRotation()
            #print("    rotation     :", rot)
            #print("    X            :", rot.X())
            #print("    Y            :", rot.Y())
            #print("    Z            :", rot.Z())
            #print("    W            :", rot.W())
            #tran = trans.TranslationPart()
            #print("    translation  :", tran)
            #print("    X            :", tran.X())
            #print("    Y            :", tran.Y())
            #print("    Z            :", tran.Z())
            c = Quantity_Color(0.5, 0.5, 0.5,
                               Quantity_TOC_RGB)  # default color
            colorSet = False
            if (color_tool.GetInstanceColor(shape, 0, c)
                    or color_tool.GetInstanceColor(shape, 1, c)
                    or color_tool.GetInstanceColor(shape, 2, c)):
                color_tool.SetInstanceColor(shape, 0, c)
                color_tool.SetInstanceColor(shape, 1, c)
                color_tool.SetInstanceColor(shape, 2, c)
                colorSet = True
                n = c.Name(c.Red(), c.Green(), c.Blue())
                print('    instance color Name & RGB: ', c, n, c.Red(),
                      c.Green(), c.Blue())

            if not colorSet:
                if (color_tool.GetColor(lab, 0, c)
                        or color_tool.GetColor(lab, 1, c)
                        or color_tool.GetColor(lab, 2, c)):

                    color_tool.SetInstanceColor(shape, 0, c)
                    color_tool.SetInstanceColor(shape, 1, c)
                    color_tool.SetInstanceColor(shape, 2, c)

                    n = c.Name(c.Red(), c.Green(), c.Blue())
                    print('    shape color Name & RGB: ', c, n, c.Red(),
                          c.Green(), c.Blue())

            shape_disp = BRepBuilderAPI_Transform(
                shape, loc.Transformation()).Shape()
            if not shape_disp in output_shapes:
                output_shapes[shape_disp] = [lab.GetLabelName(), c]
            for i in range(l_subss.Length()):
                lab_subs = l_subss.Value(i + 1)
                #print("\n########  simpleshape subshape label :", lab)
                shape_sub = shape_tool.GetShape(lab_subs)

                c = Quantity_Color(0.5, 0.5, 0.5,
                                   Quantity_TOC_RGB)  # default color
                colorSet = False
                if (color_tool.GetInstanceColor(shape_sub, 0, c)
                        or color_tool.GetInstanceColor(shape_sub, 1, c)
                        or color_tool.GetInstanceColor(shape_sub, 2, c)):
                    color_tool.SetInstanceColor(shape_sub, 0, c)
                    color_tool.SetInstanceColor(shape_sub, 1, c)
                    color_tool.SetInstanceColor(shape_sub, 2, c)
                    colorSet = True
                    n = c.Name(c.Red(), c.Green(), c.Blue())
                    print('    instance color Name & RGB: ', c, n, c.Red(),
                          c.Green(), c.Blue())

                if not colorSet:
                    if (color_tool.GetColor(lab_subs, 0, c)
                            or color_tool.GetColor(lab_subs, 1, c)
                            or color_tool.GetColor(lab_subs, 2, c)):
                        color_tool.SetInstanceColor(shape, 0, c)
                        color_tool.SetInstanceColor(shape, 1, c)
                        color_tool.SetInstanceColor(shape, 2, c)

                        n = c.Name(c.Red(), c.Green(), c.Blue())
                        print('    shape color Name & RGB: ', c, n, c.Red(),
                              c.Green(), c.Blue())
                shape_to_disp = BRepBuilderAPI_Transform(
                    shape_sub, loc.Transformation()).Shape()
                # position the subshape to display
                if not shape_to_disp in output_shapes:
                    output_shapes[shape_to_disp] = [lab_subs.GetLabelName(), c]
示例#7
0
    def parse_components(self, comps, shape_tool, color_tool):
        """Parse components from comps (LabelSequence).

        Components of an assembly are, by definition, references which refer
        to either a simple shape or a compound shape (an assembly).
        Components are essentially 'instances' of the referred shape or assembly
        and carry a location vector specifing the location of the referred
        shape or assembly."""

        for j in range(comps.Length()):
            logger.debug("Assy_entry_stack: %s", self.assy_entry_stack)
            logger.debug("loop %i of %i", j + 1, comps.Length())
            c_label = comps.Value(j + 1)  # component label <class 'TDF_Label'>
            c_name = c_label.GetLabelName()
            c_entry = c_label.EntryDumpToString()
            c_uid = self.get_uid_from_entry(c_entry)
            c_shape = shape_tool.GetShape(c_label)
            logger.debug("Component number %i", j + 1)
            logger.debug("Component name: %s", c_name)
            logger.debug("Component entry: %s", c_entry)
            ref_label = TDF_Label()  # label of referred shape (or assembly)
            is_ref = shape_tool.GetReferredShape(c_label, ref_label)
            if is_ref:  # I think all components are references
                ref_name = ref_label.GetLabelName()
                ref_shape = shape_tool.GetShape(ref_label)
                ref_entry = ref_label.EntryDumpToString()
                self.label_dict[c_uid] = {
                    'entry': c_entry,
                    'name': c_name,
                    'parent_uid': self.parent_uid_stack[-1],
                    'ref_entry': ref_entry
                }
                if shape_tool.IsSimpleShape(ref_label):
                    self.label_dict[c_uid].update({'is_assy': False})
                    temp_assy_loc_stack = list(self.assy_loc_stack)
                    # Multiply locations in stack sequentially to a result
                    if len(temp_assy_loc_stack) > 1:
                        res_loc = temp_assy_loc_stack.pop(0)
                        for loc in temp_assy_loc_stack:
                            res_loc = res_loc.Multiplied(loc)
                        c_shape.Move(res_loc)
                    elif len(temp_assy_loc_stack) == 1:
                        res_loc = temp_assy_loc_stack.pop()
                        c_shape.Move(res_loc)
                    else:
                        res_loc = None
                    # It is possible for this component to both specify a
                    # location 'c_loc' and refer directly to a top level shape.
                    # If this component *does* specify a location 'c_loc',
                    # it will be applied to the referred shape without being
                    # included in temp_assy_loc_stack. But in order to keep
                    # track of the total location from the root shape to the
                    # instance, it needs to be accounted for (by mutiplying
                    # res_loc by it) before saving it to part_dict.
                    c_loc = None
                    c_loc = shape_tool.GetLocation(c_label)
                    if c_loc:
                        loc = res_loc.Multiplied(c_loc)
                    color = Quantity_Color()
                    color_tool.GetColor(ref_shape, XCAFDoc_ColorSurf, color)
                    self.part_dict[c_uid] = {
                        'shape': c_shape,
                        'color': color,
                        'name': c_name,
                        'loc': loc
                    }
                elif shape_tool.IsAssembly(ref_label):
                    self.label_dict[c_uid].update({'is_assy': True})
                    logger.debug("Referred item is an Assembly")
                    # Location vector is carried by component
                    aLoc = TopLoc_Location()
                    aLoc = shape_tool.GetLocation(c_label)
                    self.assy_loc_stack.append(aLoc)
                    self.assy_entry_stack.append(ref_entry)
                    self.parent_uid_stack.append(c_uid)
                    r_comps = TDF_LabelSequence()  # Components of Assy
                    subchilds = False
                    isAssy = shape_tool.GetComponents(ref_label, r_comps,
                                                      subchilds)
                    logger.debug("Assy name: %s", ref_name)
                    logger.debug("Is Assembly? %s", isAssy)
                    logger.debug("Number of components: %s", r_comps.Length())
                    if r_comps.Length():
                        logger.debug("")
                        logger.debug("Parsing components of label entry %s)",
                                     ref_entry)
                        self.parse_components(r_comps, shape_tool, color_tool)
            else:
                print(
                    f"I was wrong: All components are *not* references {c_uid}"
                )
        self.assy_entry_stack.pop()
        self.assy_loc_stack.pop()
        self.parent_uid_stack.pop()
示例#8
0
        def _get_sub_shapes(lab, loc):

            l_subss = TDF_LabelSequence()
            shape_tool.GetSubShapes(lab, l_subss)
            #print("Nb subshapes   :", l_subss.Length())
            l_comps = TDF_LabelSequence()
            shape_tool.GetComponents(lab, l_comps)
            #print("Nb components  :", l_comps.Length())
            #print()
            name = lab.GetLabelName()
            print("Name :", name)

            if shape_tool.IsAssembly(lab):
                l_c = TDF_LabelSequence()
                shape_tool.GetComponents(lab, l_c)
                for i in range(l_c.Length()):
                    label = l_c.Value(i + 1)
                    if shape_tool.IsReference(label):
                        label_reference = TDF_Label()
                        shape_tool.GetReferredShape(label, label_reference)
                        loc = shape_tool.GetLocation(label)

                        locs.append(loc)
                        _get_sub_shapes(label_reference, loc)
                        locs.pop()

            elif shape_tool.IsSimpleShape(lab):
                shape = shape_tool.GetShape(lab)

                loc = TopLoc_Location()
                for l in locs:
                    loc = loc.Multiplied(l)

                c = Quantity_Color(0.5, 0.5, 0.5,
                                   Quantity_TOC_RGB)  # default color
                colorSet = False
                if (color_tool.GetInstanceColor(shape, 0, c)
                        or color_tool.GetInstanceColor(shape, 1, c)
                        or color_tool.GetInstanceColor(shape, 2, c)):
                    color_tool.SetInstanceColor(shape, 0, c)
                    color_tool.SetInstanceColor(shape, 1, c)
                    color_tool.SetInstanceColor(shape, 2, c)
                    colorSet = True
                    n = c.Name(c.Red(), c.Green(), c.Blue())
                    print('    Instance color Name & RGB: ', c, n, c.Red(),
                          c.Green(), c.Blue())

                if not colorSet:
                    if (color_tool.GetColor(lab, 0, c)
                            or color_tool.GetColor(lab, 1, c)
                            or color_tool.GetColor(lab, 2, c)):
                        color_tool.SetInstanceColor(shape, 0, c)
                        color_tool.SetInstanceColor(shape, 1, c)
                        color_tool.SetInstanceColor(shape, 2, c)

                        n = c.Name(c.Red(), c.Green(), c.Blue())
                        print('    Shape color Name & RGB: ', c, n, c.Red(),
                              c.Green(), c.Blue())

                shape_disp = BRepBuilderAPI_Transform(
                    shape, loc.Transformation()).Shape()
                if not shape_disp in output_shapes:
                    output_shapes[shape_disp] = [lab.GetLabelName(), c]

                for i in range(l_subss.Length()):
                    lab_subs = l_subss.Value(i + 1)
                    shape_sub = shape_tool.GetShape(lab_subs)

                    c = Quantity_Color(0.5, 0.5, 0.5,
                                       Quantity_TOC_RGB)  # default color
                    colorSet = False
                    if (color_tool.GetInstanceColor(shape_sub, 0, c)
                            or color_tool.GetInstanceColor(shape_sub, 1, c)
                            or color_tool.GetInstanceColor(shape_sub, 2, c)):
                        color_tool.SetInstanceColor(shape_sub, 0, c)
                        color_tool.SetInstanceColor(shape_sub, 1, c)
                        color_tool.SetInstanceColor(shape_sub, 2, c)
                        colorSet = True
                        n = c.Name(c.Red(), c.Green(), c.Blue())
                        print('    Instance color Name & RGB: ', c, n, c.Red(),
                              c.Green(), c.Blue())

                    if not colorSet:
                        if (color_tool.GetColor(lab_subs, 0, c)
                                or color_tool.GetColor(lab_subs, 1, c)
                                or color_tool.GetColor(lab_subs, 2, c)):
                            color_tool.SetInstanceColor(shape, 0, c)
                            color_tool.SetInstanceColor(shape, 1, c)
                            color_tool.SetInstanceColor(shape, 2, c)

                            n = c.Name(c.Red(), c.Green(), c.Blue())
                            print('    Shape color Name & RGB: ', c, n,
                                  c.Red(), c.Green(), c.Blue())
                    shape_to_disp = BRepBuilderAPI_Transform(
                        shape_sub, loc.Transformation()).Shape()

                    # position the subshape to display
                    if not shape_to_disp in output_shapes:
                        output_shapes[shape_to_disp] = [
                            lab_subs.GetLabelName(), c
                        ]
示例#9
0
    def findComponents(self, label,
                       comps):  # Discover Components of an Assembly
        logger.debug("")
        logger.debug("Finding components of label (entry = %s)" %
                     label.EntryDumpToString())
        for j in range(comps.Length()):
            logger.debug("loop %i of %i" % (j + 1, comps.Length()))
            cLabel = comps.Value(j + 1)
            cShape = self.shape_tool.GetShape(cLabel)
            logger.debug("Label %i - type : %s" % (j + 1, type(cLabel)))
            logger.debug("Entry: %s" % cLabel.EntryDumpToString())
            name = self.getName(cLabel)
            logger.debug("Part name: %s" % name)
            logger.debug("Is Assembly? %s" %
                         self.shape_tool.IsAssembly(cLabel))
            logger.debug("Is Component? %s" %
                         self.shape_tool.IsComponent(cLabel))
            logger.debug("Is Simple Shape? %s" %
                         self.shape_tool.IsSimpleShape(cLabel))
            logger.debug("Is Reference? %s" %
                         self.shape_tool.IsReference(cLabel))
            refLabel = TDF_Label()
            isRef = self.shape_tool.GetReferredShape(cLabel, refLabel)
            if isRef:
                refShape = self.shape_tool.GetShape(refLabel)
                refLabelEntry = refLabel.EntryDumpToString()
                logger.debug("Entry of referred shape: %s" % refLabelEntry)
                refName = self.getName(refLabel)
                logger.debug("Name of referred shape: %s" % refName)
                logger.debug("Is Assembly? %s" %
                             self.shape_tool.IsAssembly(refLabel))
                logger.debug("Is Component? %s" %
                             self.shape_tool.IsComponent(refLabel))
                logger.debug("Is Simple Shape? %s" %
                             self.shape_tool.IsSimpleShape(refLabel))
                logger.debug("Is Reference? %s" %
                             self.shape_tool.IsReference(refLabel))
                if self.shape_tool.IsSimpleShape(refLabel):
                    tempAssyLocStack = list(self.assyLocStack)
                    tempAssyLocStack.reverse()

                    for loc in tempAssyLocStack:
                        cShape.Move(loc)

                    color = self.getColor(refShape)
                    self.tree.create_node(name, self.getNewUID(),
                                          self.assyUidStack[-1], {
                                              'a': False,
                                              'l': None,
                                              'c': color,
                                              's': cShape
                                          })
                elif self.shape_tool.IsAssembly(refLabel):
                    name = self.getName(cLabel)  # Instance name
                    aLoc = TopLoc_Location()
                    aLoc = self.shape_tool.GetLocation(cLabel)
                    self.assyLocStack.append(aLoc)
                    newAssyUID = self.getNewUID()
                    self.tree.create_node(name, newAssyUID,
                                          self.assyUidStack[-1], {
                                              'a': True,
                                              'l': aLoc,
                                              'c': None,
                                              's': None
                                          })
                    self.assyUidStack.append(newAssyUID)
                    rComps = TDF_LabelSequence()  # Components of Assy
                    subchilds = False
                    isAssy = self.shape_tool.GetComponents(
                        refLabel, rComps, subchilds)
                    logger.debug("Assy name: %s" % name)
                    logger.debug("Is Assembly? %s" % isAssy)
                    logger.debug("Number of components: %s" % rComps.Length())
                    if rComps.Length():
                        self.findComponents(refLabel, rComps)
        self.assyUidStack.pop()
        self.assyLocStack.pop()
        return