(*
Copyright by:
buildingSMART International Limited, 1996-2019

Any technical documentation made available by buildingSMART International Limited
is the copyrighted work of buildingSMART International Limited and is owned by the 
buildingSMART International Limited. It may be photocopied, used in software development, 
or translated into another computer language without prior written consent from 
buildingSMART International Limited provided that full attribution is given. 
Prior written consent is required if changes are made to the technical specification.

This material is delivered to you as is and buildingSMART International Limited makes 
no warranty of any kind with regard to it, including, but not limited to, the implied 
warranties as to its accuracy or fitness for a particular purpose. Any use of the 
technical documentation or the information contained therein is at the risk of the user. 
Documentation may include technical or other inaccuracies or typographical errors. 
buildingSMART International Limited shall not be liable for errors contained therein or 
for incidental consequential damages in connection with the furnishing, performance or use 
of the material. The information contained in this document is subject to change without notice.

Issue date:
Friday, July 26, 2019

*)

SCHEMA IFCREPRESENTATIONRESOURCE;

REFERENCE FROM IFCGEOMETRYRESOURCE
(
  IfcDimensionCount,
  IfcRepresentationItem,
  IfcAxis2Placement,
  IfcDirection,
  IfcAxis2Placement2D,
  IfcAxis2Placement3D,
  IfcSameAxis2Placement,
  IfcSameCartesianPoint,
  IfcSameDirection,
  IfcSameValue,
  IfcRepresentationMap,
  IfcCurve,
  IfcSurface
);

REFERENCE FROM IFCMEASURERESOURCE
(
  IfcLabel,
  IfcText,
  IfcSiUnit,
  IfcDerivedUnit,
  IfcPositiveRatioMeasure,
  IfcIdentifier,
  IfcLengthMeasure,
  IfcReal,
  IfcNamedUnit,
  IfcLogical
);

REFERENCE FROM IFCMATERIALRESOURCE
(
  IfcMaterial
);

REFERENCE FROM IFCKERNEL
(
  IfcProduct
);

REFERENCE FROM IFCPRESENTATIONORGANIZATIONRESOURCE
(
  IfcPresentationLayerAssignment
);

REFERENCE FROM IFCPRESENTATIONAPPEARANCERESOURCE
(
  IfcNullStyle
);

REFERENCE FROM IFCEXTERNALREFERENCERESOURCE
(
  IfcClassificationReference
);

REFERENCE FROM IFCGEOMETRICMODELRESOURCE
(
  IfcGeometricSet
);

TYPE IfcGeometricProjectionEnum = ENUMERATION OF
	(GRAPH_VIEW
	,SKETCH_VIEW
	,MODEL_VIEW
	,PLAN_VIEW
	,REFLECTED_PLAN_VIEW
	,SECTION_VIEW
	,ELEVATION_VIEW
	,USERDEFINED
	,NOTDEFINED);
END_TYPE;

TYPE IfcGlobalOrLocalEnum = ENUMERATION OF
	(GLOBAL_COORDS
	,LOCAL_COORDS);
END_TYPE;

TYPE IfcCoordinateReferenceSystemSelect = SELECT
	(IfcCoordinateReferenceSystem
	,IfcGeometricRepresentationContext);
END_TYPE;

TYPE IfcProductRepresentationSelect = SELECT
	(IfcProductDefinitionShape
	,IfcRepresentationMap);
END_TYPE;

ENTITY IfcCoordinateOperation
 ABSTRACT SUPERTYPE OF (ONEOF
    (IfcMapConversion));
	SourceCRS : IfcCoordinateReferenceSystemSelect;
	TargetCRS : IfcCoordinateReferenceSystem;
END_ENTITY;

ENTITY IfcCoordinateReferenceSystem
 ABSTRACT SUPERTYPE OF (ONEOF
    (IfcProjectedCRS));
	Name : IfcLabel;
	Description : OPTIONAL IfcText;
	GeodeticDatum : OPTIONAL IfcIdentifier;
	VerticalDatum : OPTIONAL IfcIdentifier;
 INVERSE
	HasCoordinateOperation : SET [0:1] OF IfcCoordinateOperation FOR SourceCRS;
END_ENTITY;

ENTITY IfcGeometricRepresentationContext
 SUPERTYPE OF (ONEOF
    (IfcGeometricRepresentationSubContext))
 SUBTYPE OF (IfcRepresentationContext);
	CoordinateSpaceDimension : IfcDimensionCount;
	Precision : OPTIONAL IfcReal;
	WorldCoordinateSystem : IfcAxis2Placement;
	TrueNorth : OPTIONAL IfcDirection;
 INVERSE
	HasSubContexts : SET [0:?] OF IfcGeometricRepresentationSubContext FOR ParentContext;
	HasCoordinateOperation : SET [0:1] OF IfcCoordinateOperation FOR SourceCRS;
 WHERE
	North2D : NOT(EXISTS(TrueNorth)) OR (HIINDEX(TrueNorth.DirectionRatios) = 2);
END_ENTITY;

ENTITY IfcGeometricRepresentationSubContext
 SUBTYPE OF (IfcGeometricRepresentationContext);
	ParentContext : IfcGeometricRepresentationContext;
	TargetScale : OPTIONAL IfcPositiveRatioMeasure;
	TargetView : IfcGeometricProjectionEnum;
	UserDefinedTargetView : OPTIONAL IfcLabel;
 DERIVE
	SELF\IfcGeometricRepresentationContext.WorldCoordinateSystem : IfcAxis2Placement := ParentContext.WorldCoordinateSystem;
	SELF\IfcGeometricRepresentationContext.CoordinateSpaceDimension : IfcDimensionCount := ParentContext.CoordinateSpaceDimension;
	SELF\IfcGeometricRepresentationContext.TrueNorth : IfcDirection := NVL(ParentContext.TrueNorth, IfcConvertDirectionInto2D(SELF\IfcGeometricRepresentationContext.WorldCoordinateSystem.P[2]));
	SELF\IfcGeometricRepresentationContext.Precision : IfcReal := NVL(ParentContext.Precision,1.E-5);
 WHERE
	ParentNoSub : NOT('IFCREPRESENTATIONRESOURCE.IFCGEOMETRICREPRESENTATIONSUBCONTEXT' IN TYPEOF(ParentContext));
	UserTargetProvided :  (TargetView <> IfcGeometricProjectionEnum.USERDEFINED) OR 
((TargetView =  IfcGeometricProjectionEnum.USERDEFINED) AND EXISTS(UserDefinedTargetView)) ;
	NoCoordOperation : SIZEOF(SELF\IfcGeometricRepresentationContext.HasCoordinateOperation) = 0;
END_ENTITY;

ENTITY IfcMapConversion
 SUBTYPE OF (IfcCoordinateOperation);
	Eastings : IfcLengthMeasure;
	Northings : IfcLengthMeasure;
	OrthogonalHeight : IfcLengthMeasure;
	XAxisAbscissa : OPTIONAL IfcReal;
	XAxisOrdinate : OPTIONAL IfcReal;
	Scale : OPTIONAL IfcReal;
END_ENTITY;

ENTITY IfcMaterialDefinitionRepresentation
 SUBTYPE OF (IfcProductRepresentation);
	RepresentedMaterial : IfcMaterial;
 WHERE
	OnlyStyledRepresentations : SIZEOF(QUERY(temp <* Representations | 
  (NOT('IFCREPRESENTATIONRESOURCE.IFCSTYLEDREPRESENTATION' IN TYPEOF(temp)))
)) = 0;
END_ENTITY;

ENTITY IfcProductDefinitionShape
 SUBTYPE OF (IfcProductRepresentation);
 INVERSE
	ShapeOfProduct : SET [1:?] OF IfcProduct FOR Representation;
	HasShapeAspects : SET [0:?] OF IfcShapeAspect FOR PartOfProductDefinitionShape;
 WHERE
	OnlyShapeModel : SIZEOF(QUERY(temp <* Representations | 
  (NOT('IFCREPRESENTATIONRESOURCE.IFCSHAPEMODEL' IN TYPEOF(temp)))
)) = 0;
END_ENTITY;

ENTITY IfcProductRepresentation
 ABSTRACT SUPERTYPE OF (ONEOF
    (IfcMaterialDefinitionRepresentation
    ,IfcProductDefinitionShape));
	Name : OPTIONAL IfcLabel;
	Description : OPTIONAL IfcText;
	Representations : LIST [1:?] OF IfcRepresentation;
END_ENTITY;

ENTITY IfcProjectedCRS
 SUBTYPE OF (IfcCoordinateReferenceSystem);
	MapProjection : OPTIONAL IfcIdentifier;
	MapZone : OPTIONAL IfcIdentifier;
	MapUnit : OPTIONAL IfcNamedUnit;
 WHERE
	IsLengthUnit : NOT(EXISTS(MapUnit)) OR (MapUnit.UnitType = IfcUnitEnum.LENGTHUNIT);
END_ENTITY;

ENTITY IfcRepresentation
 ABSTRACT SUPERTYPE OF (ONEOF
    (IfcShapeModel
    ,IfcStyleModel));
	ContextOfItems : IfcRepresentationContext;
	RepresentationIdentifier : OPTIONAL IfcLabel;
	RepresentationType : OPTIONAL IfcLabel;
	Items : SET [1:?] OF IfcRepresentationItem;
 INVERSE
	RepresentationMap : SET [0:1] OF IfcRepresentationMap FOR MappedRepresentation;
	LayerAssignments : SET [0:?] OF IfcPresentationLayerAssignment FOR AssignedItems;
	OfProductRepresentation : SET [0:?] OF IfcProductRepresentation FOR Representations;
END_ENTITY;

ENTITY IfcRepresentationContext
 ABSTRACT SUPERTYPE OF (ONEOF
    (IfcGeometricRepresentationContext));
	ContextIdentifier : OPTIONAL IfcLabel;
	ContextType : OPTIONAL IfcLabel;
 INVERSE
	RepresentationsInContext : SET [0:?] OF IfcRepresentation FOR ContextOfItems;
END_ENTITY;

ENTITY IfcShapeAspect;
	ShapeRepresentations : LIST [1:?] OF IfcShapeModel;
	Name : OPTIONAL IfcLabel;
	Description : OPTIONAL IfcText;
	ProductDefinitional : IfcLogical;
	PartOfProductDefinitionShape : OPTIONAL IfcProductRepresentationSelect;
END_ENTITY;

ENTITY IfcShapeModel
 ABSTRACT SUPERTYPE OF (ONEOF
    (IfcShapeRepresentation
    ,IfcTopologyRepresentation))
 SUBTYPE OF (IfcRepresentation);
 INVERSE
	OfShapeAspect : SET [0:1] OF IfcShapeAspect FOR ShapeRepresentations;
 WHERE
	WR11 : (SIZEOF(SELF\IfcRepresentation.OfProductRepresentation) = 1) XOR 
(SIZEOF(SELF\IfcRepresentation.RepresentationMap) = 1) XOR
(SIZEOF(OfShapeAspect) = 1);
END_ENTITY;

ENTITY IfcShapeRepresentation
 SUBTYPE OF (IfcShapeModel);
 WHERE
	CorrectContext : 'IFCREPRESENTATIONRESOURCE.IFCGEOMETRICREPRESENTATIONCONTEXT' 
IN TYPEOF(SELF\IfcRepresentation.ContextOfItems);
	NoTopologicalItem : SIZEOF(QUERY(temp <* Items | 
  ('IFCTOPOLOGYRESOURCE.IFCTOPOLOGICALREPRESENTATIONITEM' IN TYPEOF(temp))
    AND (NOT(SIZEOF(
      ['IFCTOPOLOGYRESOURCE.IFCVERTEXPOINT',
       'IFCTOPOLOGYRESOURCE.IFCEDGECURVE',
       'IFCTOPOLOGYRESOURCE.IFCFACESURFACE'] * TYPEOF(temp)) = 1))
)) = 0;
	HasRepresentationType : EXISTS(SELF\IfcRepresentation.RepresentationType);
	HasRepresentationIdentifier : EXISTS(SELF\IfcRepresentation.RepresentationIdentifier);
	CorrectItemsForType : IfcShapeRepresentationTypes(SELF\IfcRepresentation.RepresentationType, SELF\IfcRepresentation.Items);
END_ENTITY;

ENTITY IfcStyleModel
 ABSTRACT SUPERTYPE OF (ONEOF
    (IfcStyledRepresentation))
 SUBTYPE OF (IfcRepresentation);
END_ENTITY;

ENTITY IfcStyledRepresentation
 SUBTYPE OF (IfcStyleModel);
 WHERE
	OnlyStyledItems : SIZEOF(QUERY(temp <* SELF\IfcRepresentation.Items | 
  (NOT('IFCPRESENTATIONAPPEARANCERESOURCE.IFCSTYLEDITEM' IN TYPEOF(temp)))
)) = 0;
END_ENTITY;

ENTITY IfcTopologyRepresentation
 SUBTYPE OF (IfcShapeModel);
 WHERE
	WR21 : SIZEOF(QUERY(temp <* SELF\IfcRepresentation.Items | 
  NOT('IFCTOPOLOGYRESOURCE.IFCTOPOLOGICALREPRESENTATIONITEM' IN TYPEOF(temp))
)) = 0

;
	WR22 : EXISTS(SELF\IfcRepresentation.RepresentationType);
	WR23 : IfcTopologyRepresentationTypes(SELF\IfcRepresentation.RepresentationType, SELF\IfcRepresentation.Items);
END_ENTITY;

FUNCTION IfcConvertDirectionInto2D
    (Direction : IfcDirection)
    : IfcDirection;
	
  LOCAL
    Direction2D : IfcDirection := IfcRepresentationItem() || IfcGeometricRepresentationItem () || IfcDirection([0.,1.]);
  END_LOCAL;
  
  Direction2D.DirectionRatios[1] := Direction.DirectionRatios[1];
  Direction2D.DirectionRatios[2] := Direction.DirectionRatios[2];
  
  RETURN (Direction2D);

END_FUNCTION;

FUNCTION IfcSameValidPrecision
   (Epsilon1, Epsilon2 : REAL) : LOGICAL ;
  LOCAL
    ValidEps1, ValidEps2 : REAL;
    DefaultEps           : REAL := 0.000001;
    DerivationOfEps      : REAL := 1.001;
    UpperEps             : REAL := 1.0;
  END_LOCAL;
  
    ValidEps1 := NVL(Epsilon1, DefaultEps);
    ValidEps2 := NVL(Epsilon2, DefaultEps);
    RETURN ((0.0 < ValidEps1) AND (ValidEps1 <= (DerivationOfEps * ValidEps2)) AND 
            (ValidEps2 <= (DerivationOfEps * ValidEps1)) AND (ValidEps2 < UpperEps));

END_FUNCTION;

FUNCTION IfcShapeRepresentationTypes
(RepType : IfcLabel; Items : SET OF IfcRepresentationItem) : LOGICAL;
    
    LOCAL
      Count : INTEGER := 0;
    END_LOCAL;

    CASE RepType OF 
    'Point' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCPOINT' IN TYPEOF(temp))));
      END;

    'PointCloud' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCCARTESIANPOINTLIST3D' IN TYPEOF(temp))));
      END;

    'Curve' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCCURVE' IN TYPEOF(temp))));
      END;

    'Curve2D' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCCURVE' IN TYPEOF(temp)) 
                 AND (temp\IfcCurve.Dim = 2)));
      END;

    'Curve3D' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCCURVE' IN TYPEOF(temp)) 
                 AND (temp\IfcCurve.Dim = 3)));
      END;

    'Surface' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCSURFACE' IN TYPEOF(temp))));
      END;

    'Surface2D' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCSURFACE' IN TYPEOF(temp)) 
                 AND (temp\IfcSurface.Dim = 2)));
      END;

    'Surface3D' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCSURFACE' IN TYPEOF(temp)) 
                 AND (temp\IfcSurface.Dim = 3)));
      END;

    'FillArea' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCANNOTATIONFILLAREA' IN TYPEOF(temp))));
      END;

    'Text' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCTEXTLITERAL' IN TYPEOF(temp))));
      END;

    'AdvancedSurface' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | 'IFCGEOMETRYRESOURCE.IFCBSPLINESURFACE' IN TYPEOF(temp)));
      END;

    'Annotation2D' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | (
                  SIZEOF(TYPEOF(temp) * [
                   'IFCGEOMETRYRESOURCE.IFCPOINT',
                   'IFCGEOMETRYRESOURCE.IFCCURVE',
                   'IFCGEOMETRICMODELRESOURCE.IFCGEOMETRICCURVESET',
                   'IFCPRESENTATIONDEFINITIONRESOURCE.IFCANNOTATIONFILLAREA',
                   'IFCPRESENTATIONDEFINITIONRESOURCE.IFCTEXTLITERAL']) = 1)
                 ));
      END;

    'GeometricSet' : 
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRICMODELRESOURCE.IFCGEOMETRICSET' IN TYPEOF(temp))
                 OR ('IFCGEOMETRYRESOURCE.IFCPOINT' IN TYPEOF(temp))
                 OR ('IFCGEOMETRYRESOURCE.IFCCURVE' IN TYPEOF(temp))
                 OR ('IFCGEOMETRYRESOURCE.IFCSURFACE' IN TYPEOF(temp))));
      END;

    'GeometricCurveSet' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRICMODELRESOURCE.IFCGEOMETRICCURVESET' IN TYPEOF(temp))
                 OR ('IFCGEOMETRICMODELRESOURCE.IFCGEOMETRICSET' IN TYPEOF(temp))
                 OR ('IFCGEOMETRYRESOURCE.IFCPOINT' IN TYPEOF(temp))
                 OR ('IFCGEOMETRYRESOURCE.IFCCURVE' IN TYPEOF(temp))));
         REPEAT i:=1 TO HIINDEX(Items);  
           IF ('IFCGEOMETRYRESOURCE.IFCGEOMETRICSET' IN TYPEOF(Items[i]))
           THEN
             IF (SIZEOF(QUERY(temp <* Items[i]\IfcGeometricSet.Elements | 'IFCGEOMETRYRESOURCE.IFCSURFACE' IN TYPEOF(temp))) > 0)
             THEN
               Count := Count - 1;
             END_IF;
           END_IF;
         END_REPEAT;
      END;

    'Tessellation' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | 'IFCGEOMETRICMODELRESOURCE.IFCTESSELLATEDITEM' IN TYPEOF(temp)));
      END;

    'SurfaceOrSolidModel' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | SIZEOF([
                   'IFCGEOMETRICMODELRESOURCE.IFCTESSELLATEDITEM',
                   'IFCGEOMETRICMODELRESOURCE.IFCSHELLBASEDSURFACEMODEL',
                   'IFCGEOMETRICMODELRESOURCE.IFCFACEBASEDSURFACEMODEL',
                   'IFCGEOMETRICMODELRESOURCE.IFCSOLIDMODEL'] * TYPEOF(temp)) >= 1
                 ));      
      END;

    'SurfaceModel' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | SIZEOF([
                   'IFCGEOMETRICMODELRESOURCE.IFCTESSELLATEDITEM',
                   'IFCGEOMETRICMODELRESOURCE.IFCSHELLBASEDSURFACEMODEL',
                   'IFCGEOMETRICMODELRESOURCE.IFCFACEBASEDSURFACEMODEL'] * TYPEOF(temp)) >= 1
                 ));      
      END;

    'SolidModel' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRICMODELRESOURCE.IFCSOLIDMODEL' IN TYPEOF(temp))));            
      END;

    'SweptSolid' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | (SIZEOF([
                   'IFCGEOMETRICMODELRESOURCE.IFCEXTRUDEDAREASOLID',
                   'IFCGEOMETRICMODELRESOURCE.IFCREVOLVEDAREASOLID'] * TYPEOF(temp)) >= 1
                   ) AND (SIZEOF([
                   'IFCGEOMETRICMODELRESOURCE.IFCEXTRUDEDAREASOLIDTAPERED',
                   'IFCGEOMETRICMODELRESOURCE.IFCREVOLVEDAREASOLIDTAPERED'] * TYPEOF(temp)) = 0
                   )
                 ));                             
      END;

    'AdvancedSweptSolid' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | SIZEOF([
                   'IFCGEOMETRICMODELRESOURCE.IFCSWEPTAREASOLID',
                   'IFCGEOMETRICMODELRESOURCE.IFCSWEPTDISKSOLID'] *  TYPEOF(temp)) >= 1
                 ));      
      END;

    'CSG' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | SIZEOF([
                   'IFCGEOMETRICMODELRESOURCE.IFCBOOLEANRESULT',
                   'IFCGEOMETRICMODELRESOURCE.IFCCSGPRIMITIVE3D',
                   'IFCGEOMETRICMODELRESOURCE.IFCCSGSOLID'] *  TYPEOF(temp)) >= 1
                 ));            
      END;

    'Clipping' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | SIZEOF([
                   'IFCGEOMETRICMODELRESOURCE.IFCCSGSOLID',
                   'IFCGEOMETRICMODELRESOURCE.IFCBOOLEANCLIPPINGRESULT'] * TYPEOF(temp)) >= 1
                 )); 
      END;

    'Brep' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRICMODELRESOURCE.IFCFACETEDBREP' IN TYPEOF(temp))));      
      END;

    'AdvancedBrep' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRICMODELRESOURCE.IFCMANIFOLDSOLIDBREP' IN TYPEOF(temp))));      
      END;

    'BoundingBox' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRICMODELRESOURCE.IFCBOUNDINGBOX' IN TYPEOF(temp))));
        IF (SIZEOF(Items) > 1)
        THEN
          Count := 0;
        END_IF;   
      END;

    'SectionedSpine' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRICMODELRESOURCE.IFCSECTIONEDSPINE' IN TYPEOF(temp))));      
      END;

    'LightSource' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | ('IFCPRESENTATIONORGANIZATIONRESOURCE.IFCLIGHTSOURCE' IN TYPEOF(temp))));      
      END;

    'MappedRepresentation' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | ('IFCGEOMETRYRESOURCE.IFCMAPPEDITEM' IN TYPEOF(temp))));  
      END;

     OTHERWISE : RETURN(?);
    END_CASE;
    RETURN (Count = SIZEOF(Items));
END_FUNCTION;

FUNCTION IfcTopologyRepresentationTypes
(RepType : IfcLabel; Items : SET OF IfcRepresentationItem) : LOGICAL;
    
    LOCAL
      Count : INTEGER := 0;
    END_LOCAL;

    CASE RepType OF 
    'Vertex' :
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | 
                  ('IFCTOPOLOGYRESOURCE.IFCVERTEX' IN TYPEOF(temp))));
      END;
    'Edge' : 
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | 
                  ('IFCTOPOLOGYRESOURCE.IFCEDGE' IN TYPEOF(temp))));
      END;
    'Path' : 
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | 
                  ('IFCTOPOLOGYRESOURCE.IFCPATH' IN TYPEOF(temp))));
      END;
    'Face' : 
      BEGIN 
        Count := SIZEOF(QUERY(temp <* Items | 
                  ('IFCTOPOLOGYRESOURCE.IFCFACE' IN TYPEOF(temp))));
      END;
    'Shell' :
      BEGIN
        Count := SIZEOF(QUERY(temp <* Items | 
                  ('IFCTOPOLOGYRESOURCE.IFCOPENSHELL' IN TYPEOF(temp))
                    OR ('IFCTOPOLOGYRESOURCE.IFCCLOSEDSHELL' IN TYPEOF(temp))));
      END;
    'Undefined': RETURN(TRUE);
     OTHERWISE : RETURN(?);
    END_CASE;
    RETURN (Count = SIZEOF(Items));
END_FUNCTION;

RULE IfcRepresentationContextSameWCS FOR
	(IfcGeometricRepresentationContext);
LOCAL
  IsDifferent  : LOGICAL := FALSE;
END_LOCAL;
  IF (SIZEOF(IfcGeometricRepresentationContext) > 1)
  THEN
    REPEAT i := 2 TO HIINDEX(IfcGeometricRepresentationContext);
      IF (IfcGeometricRepresentationContext[1].WorldCoordinateSystem :<>: IfcGeometricRepresentationContext[i].WorldCoordinateSystem)
      THEN
        IsDifferent := (NOT(IfcSameValidPrecision(IfcGeometricRepresentationContext[1].Precision,
                                                  IfcGeometricRepresentationContext[i].Precision)))
                    OR (NOT(IfcSameAxis2Placement(IfcGeometricRepresentationContext[1].WorldCoordinateSystem,
                                                  IfcGeometricRepresentationContext[i].WorldCoordinateSystem,
                                                  IfcGeometricRepresentationContext[1].Precision)));
        IF (IsDifferent = TRUE) THEN
          ESCAPE;
        END_IF;
      END_IF;
    END_REPEAT;
  END_IF;
    WHERE
      WR1 : IsDifferent = FALSE;
END_RULE;

END_SCHEMA;
