(*
 * $Id$
 *
 * JSDAI(TM), a way to implement STEP, ISO 10303
 * Copyright (C) 1997-2008, LKSoftWare GmbH, Germany
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License
 * version 3 as published by the Free Software Foundation (AGPL v3).
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * JSDAI is a registered trademark of LKSoftWare GmbH, Germany
 * This software is also available under commercial licenses.
 * See also http://www.jsdai.net/
 *)

(*
   Derived from ISO TC184/SC4/WG12 N - ISO/CD-TS 10303-xxxx Interconnect 2d shape - EXPRESS ARM
   by LKSoftWare GmbH for implementation purpose within IDA-STEP(TM)
*)

SCHEMA Interconnect_2d_shape_xim;

USE FROM Fabrication_joint_xim;

USE FROM Physical_unit_2d_design_view_xim;

USE FROM Interconnect_2d_shape_mim;

REFERENCE FROM support_resource_schema   -- ISO 10303-41
  (bag_to_set);

  ENTITY Component_2d_embedded_location
    (* CONNOTATIONAL *) SUBTYPE OF (Component_2d_location_armx);
      precedent_surface  : Adjacent_stratum_surface_embedded_physical_component_surface_definition;
      subsequent_surface : Adjacent_stratum_surface_embedded_physical_component_surface_definition;
      reference_terminal : Fabrication_joint_armx;
    DERIVE
      SELF\Representation_relationship.description : text := 'component embedded';
    UNIQUE
      UR1 : precedent_surface,
 reference_terminal,
 subsequent_surface;
    WHERE
      WR1 : precedent_surface <> subsequent_surface;
      WR2 : SELF\Component_2d_location_armx.substrate_location = FALSE;
  END_ENTITY;
(* As almost agreed in issue #2158 from SEDSZilla, remove this entity
  ENTITY Continuous_assembly_component_2d_shape_model
   SUBTYPE OF (Assembly_component_2d_shape_model);
      SELF\Assembly_component_2d_shape_model.shape_characterized_component : SET[1:?] OF Connected_area_component_armx;
  END_ENTITY;
*)
RULE no_component_assembly_2d_location_for_area_component FOR (Area_component);
WHERE
  WR1:  SIZEOF(QUERY( ac <* Area_component | NOT(
       (SIZEOF(USEDIN(ac,'PHYSICAL_UNIT_2D_DESIGN_VIEW_ARM.ASSEMBLY_COMPONENT_2D_SHAPE_MODEL.SHAPE_CHARACTERIZED_COMPONENT')) = 0) OR
       (no_2d_location_for_component(ac))) 
  )) = 0;
END_RULE;  

RULE component_in_assembly_2d_context FOR (Area_component);
WHERE
  WR1:  SIZEOF(QUERY( ac <* Area_component | NOT(
       (SIZEOF(USEDIN(ac,'PHYSICAL_UNIT_2D_DESIGN_VIEW_ARM.ASSEMBLY_COMPONENT_2D_SHAPE_MODEL.SHAPE_CHARACTERIZED_COMPONENT')) = 0) OR 
        (in_2d_assembly_context(ac)))
          )) = 0;
END_RULE;  

FUNCTION in_2d_assembly_context(comp : Assembly_component_armx) : LOGICAL;
  LOCAL
    shapes : SET OF Assembly_component_2d_shape_model := [];
    shapes_in_context : SET OF Physical_unit_planar_shape_model := [];
    assemblies : SET OF Product_view_definition := [];
    i, j : INTEGER := 0;
    in_context : LOGICAL := TRUE;
  END_LOCAL;
  
  REPEAT i := 1 TO SIZEOF(comp.occurrence_contexts);
    assemblies := assemblies + comp.occurrence_contexts[i].relating_product_definition;
  END_REPEAT;

  shapes := bag_to_set(USEDIN(comp,'PHYSICAL_UNIT_2D_DESIGN_VIEW_XIM.ASSEMBLY_COMPONENT_2D_SHAPE_MODEL.SHAPE_CHARACTERIZED_COMPONENT'));
  REPEAT i := 1 TO SIZEOF(shapes);
    shapes_in_context := bag_to_set(USEDIN(shapes[i]\Shape_representation.context_of_items,'ELEMENTAL_GEOMETRIC_SHAPE_XIM.SHAPE_REPRESENTATION.CONTEXT_OF_ITEMS'));
    in_context := FALSE;
    j := 1;
    REPEAT WHILE ((j <= SIZEOF(shapes_in_context)) AND (NOT(in_context)));
      in_context := (shapes_in_context[j].shape_characterized_definition IN assemblies);
      j := j + 1;
    END_REPEAT;
    
    IF NOT (in_context) THEN
      RETURN (FALSE);
    END_IF;
  END_REPEAT;
  
  RETURN (SIZEOF(shapes) > 0);
END_FUNCTION;

FUNCTION no_2d_location_for_component(comp : Assembly_component_armx) : BOOLEAN;
  LOCAL
    i, j : INTEGER;
    cis : SET OF Contextual_item_shape;
    loc : SET OF Component_2d_location_armx;
  END_LOCAL;
  
  REPEAT i := 1 TO SIZEOF(comp.occurrence_contexts);
    cis := bag_to_set(USEDIN(comp.occurrence_contexts[i],'SHAPE_PROPERTY_ASSIGNMENT_XIM.CONTEXTUAL_ITEM_SHAPE.DESCRIBED_ELEMENT'));
    REPEAT j := 1 TO SIZEOF(cis);
      loc := bag_to_set(USEDIN(cis[j],'CONTEXTUAL_SHAPE_POSITIONING_XIM.CONTEXTUAL_SHAPE_REPRESENTATION.CONTEXTUAL_SHAPE'));
      IF (SIZEOF(loc) > 0) THEN
        RETURN (FALSE);
      END_IF;
    END_REPEAT;
  END_REPEAT;

  RETURN (TRUE);
END_FUNCTION;

END_SCHEMA;

