parameters: (self, offset=0)
return_statement: return True
docstring: Align the plane to a selection if it defines a plane.
  
          If the selection uniquely defines a plane it will be used.
  
          Parameter
          ---------
          offset : float
              Defaults to zero. A value which will be used to offset
              the plane in the direction of its `axis`.
  
          Returns
          -------
          bool
              `True` if the operation was successful, and `False` otherwise.
              It returns `False` if the selection has no elements,
              or if the object is not derived from `'Part::Feature'`
              or if the object doesn't have a `Shape`.
  
          See Also
          --------
          alignToFace, alignToCurve
docstring_summary: Align the plane to a selection if it defines a plane.
docstring_tokens: Align the plane to a selection if it defines a plane .
function: def alignToSelection(self, offset=0):
          """Align the plane to a selection if it defines a plane.
  
          If the selection uniquely defines a plane it will be used.
  
          Parameter
          ---------
          offset : float
              Defaults to zero. A value which will be used to offset
              the plane in the direction of its `axis`.
  
          Returns
          -------
          bool
              `True` if the operation was successful, and `False` otherwise.
              It returns `False` if the selection has no elements,
              or if the object is not derived from `'Part::Feature'`
              or if the object doesn't have a `Shape`.
  
          See Also
          --------
          alignToFace, alignToCurve
          """
          sel_ex = FreeCADGui.Selection.getSelectionEx(FreeCAD.ActiveDocument.Name)
          if not sel_ex:
              return False
  
          shapes = list()
          names = list()
          for obj in sel_ex:
              # check that the geometric property is a Part.Shape object
              geom_is_shape = False
              if isinstance(obj.Object, FreeCAD.GeoFeature):
                  geom = obj.Object.getPropertyOfGeometry()
                  if isinstance(geom, Part.Shape):
                      geom_is_shape = True
              if not geom_is_shape:
                  FreeCAD.Console.PrintError(translate(
                      "draft",
                      "Object without Part.Shape geometry:'{}'".format(
                          obj.ObjectName)) + "\n")
                  return False
              if geom.isNull():
                  FreeCAD.Console.PrintError(translate(
                      "draft",
                      "Object with null Part.Shape geometry:'{}'".format(
                          obj.ObjectName)) + "\n")
                  return False
              if obj.HasSubObjects:
                  shapes.extend(obj.SubObjects)
                  names.extend([obj.ObjectName + "." + n for n in obj.SubElementNames])
              else:
                  shapes.append(geom)
                  names.append(obj.ObjectName)
  
          normal = None
          for n in range(len(shapes)):
              if not DraftGeomUtils.is_planar(shapes[n]):
                  FreeCAD.Console.PrintError(translate(
                     "draft", "'{}' object is not planar".format(names[n])) + "\n")
                  return False
              if not normal:
                  normal = DraftGeomUtils.get_normal(shapes[n])
                  shape_ref = n
  
          # test if all shapes are coplanar
          if normal:
              for n in range(len(shapes)):
                  if not DraftGeomUtils.are_coplanar(shapes[shape_ref], shapes[n]):
                      FreeCAD.Console.PrintError(translate(
                          "draft", "{} and {} aren't coplanar".format(
                          names[shape_ref],names[n])) + "\n")
                      return False
          else:
              # suppose all geometries are straight lines or points
              points = [vertex.Point for shape in shapes for vertex in shape.Vertexes]
              if len(points) >= 3:
                  poly = Part.makePolygon(points)
                  if not DraftGeomUtils.is_planar(poly):
                      FreeCAD.Console.PrintError(translate(
                          "draft", "All Shapes must be coplanar") + "\n")
                      return False
                  normal = DraftGeomUtils.get_normal(poly)
              else:
                  normal = None
  
          if not normal:
              FreeCAD.Console.PrintError(translate(
                  "draft", "Selected Shapes must define a plane") + "\n")
              return False
  
          # set center of mass
          ctr_mass = FreeCAD.Vector(0,0,0)
          ctr_pts = FreeCAD.Vector(0,0,0)
          mass = 0
          for shape in shapes:
              if hasattr(shape, "CenterOfMass"):
                  ctr_mass += shape.CenterOfMass*shape.Mass
                  mass += shape.Mass
              else:
                  ctr_pts += shape.Point
          if mass > 0:
              ctr_mass /= mass
          # all shapes are vertexes
          else:
              ctr_mass = ctr_pts/len(shapes)
  
          self.alignToPointAndAxis(ctr_mass, normal, offset)
  
          return True
function_tokens: def alignToSelection ( self , offset = 0 ) : sel_ex = FreeCADGui . Selection . getSelectionEx ( FreeCAD . ActiveDocument . Name ) if not sel_ex : return False shapes = list ( ) names = list ( ) for obj in sel_ex : # check that the geometric property is a Part.Shape object geom_is_shape = False if isinstance ( obj . Object , FreeCAD . GeoFeature ) : geom = obj . Object . getPropertyOfGeometry ( ) if isinstance ( geom , Part . Shape ) : geom_is_shape = True if not geom_is_shape : FreeCAD . Console . PrintError ( translate ( "draft" , "Object without Part.Shape geometry:'{}'" . format ( obj . ObjectName ) ) + "\n" ) return False if geom . isNull ( ) : FreeCAD . Console . PrintError ( translate ( "draft" , "Object with null Part.Shape geometry:'{}'" . format ( obj . ObjectName ) ) + "\n" ) return False if obj . HasSubObjects : shapes . extend ( obj . SubObjects ) names . extend ( [ obj . ObjectName + "." + n for n in obj . SubElementNames ] ) else : shapes . append ( geom ) names . append ( obj . ObjectName ) normal = None for n in range ( len ( shapes ) ) : if not DraftGeomUtils . is_planar ( shapes [ n ] ) : FreeCAD . Console . PrintError ( translate ( "draft" , "'{}' object is not planar" . format ( names [ n ] ) ) + "\n" ) return False if not normal : normal = DraftGeomUtils . get_normal ( shapes [ n ] ) shape_ref = n # test if all shapes are coplanar if normal : for n in range ( len ( shapes ) ) : if not DraftGeomUtils . are_coplanar ( shapes [ shape_ref ] , shapes [ n ] ) : FreeCAD . Console . PrintError ( translate ( "draft" , "{} and {} aren't coplanar" . format ( names [ shape_ref ] , names [ n ] ) ) + "\n" ) return False else : # suppose all geometries are straight lines or points points = [ vertex . Point for shape in shapes for vertex in shape . Vertexes ] if len ( points ) >= 3 : poly = Part . makePolygon ( points ) if not DraftGeomUtils . is_planar ( poly ) : FreeCAD . Console . PrintError ( translate ( "draft" , "All Shapes must be coplanar" ) + "\n" ) return False normal = DraftGeomUtils . get_normal ( poly ) else : normal = None if not normal : FreeCAD . Console . PrintError ( translate ( "draft" , "Selected Shapes must define a plane" ) + "\n" ) return False # set center of mass ctr_mass = FreeCAD . Vector ( 0 , 0 , 0 ) ctr_pts = FreeCAD . Vector ( 0 , 0 , 0 ) mass = 0 for shape in shapes : if hasattr ( shape , "CenterOfMass" ) : ctr_mass += shape . CenterOfMass * shape . Mass mass += shape . Mass else : ctr_pts += shape . Point if mass > 0 : ctr_mass /= mass # all shapes are vertexes else : ctr_mass = ctr_pts / len ( shapes ) self . alignToPointAndAxis ( ctr_mass , normal , offset ) return True