tpaviot / pythonocc-core

Python package for 3D geometry CAD/BIM/CAM
GNU Lesser General Public License v3.0
1.39k stars 379 forks source link

shape_tool.IsAssembly(label) returns invalid value? #1319

Open zero-kappa opened 6 months ago

zero-kappa commented 6 months ago

Hello, I am trying to parse a step file hierarchy with XCAFDoc. However, I found that some assemblies are not correctly recognized. For instance, here below [C-400-Shasi] is an assembly in freeCAD, but not an assembly in my sample code implemented with pythonOCC. image image

Here's the sample codes:


  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)
                  logger.info("Is compound? %s ", self.shape_tool.IsCompound(refLabel))
                  logger.info("Number of Components %s ", self.shape_tool.NbComponents(refLabel))
                  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())

                  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()
tpaviot commented 6 months ago

can you please include the test step file

zero-kappa commented 6 months ago

@tpaviot Sorry for late response. Due to certain constraints, I am unable to provide the STEP file for the original example. However, I have included an another STEP file where the same issue is present: image The test step file is included in the following .zip file: test_step.zip