Cadvance Programming
Urban Planning application sample source code

The following Delphi source code illustrates Cadvance data extraction techniques used to create the model illustrated below. The Cadvance drawing was used to create a 4D CAD model in the Visual Project Scheduler, which provided animation and scheduling capabilities for an Urban Planning application. The code has many examples of Delphi coding techniques useful for Cadvance applications.
 
Cadvance Visual Project Scheduler

unit VPSExport;

interface

uses
  SysUtils,
  Windows,
  Classes,
  Dialogs,
  Cditypes,
  Cdiprocs,
  Cdiprocs3D,
  Cdiconst,
  DateUtils,
  Variants,
  AdoDb,
  Db,
  DbTables,
  CadvanceUtils,
  LayerUtilities;

  procedure ExportSubModel;stdcall;
  procedure SaveSubmodelLayers(const submodel:string; const revision:string ; const ConnString:string);
  procedure SaveMemberData(const basepoint:CDI_DDD_POINT; const master_segid:CDI_SEGID; const AutoObjectNumber:longint;const ADODataSet:TADODataset);
  function ClockWise(var xp:array of World;var yp:array of World; lvertex:smallint):boolean;

implementation

function ClockWise(var xp:array of World;var yp:array of World; lvertex:smallint):boolean;
var
  area:double;
  vertex:smallint;
  vx1:double;
  vx2:double;
  vy1:double;
  vy2:double;

begin

  area:=0;
  for vertex:= 0 to lvertex-1 do
    begin
      vx1:=xp[vertex];
      vx2:=xp[vertex+1];
      vy1:=yp[vertex];
      vy2:=yp[vertex+1];
      area:=area + vx1 * vy2 - vx2 * vy1;
    end;

  if area<0 then
    begin
      result:=true;
    end
  else
    begin
      result:=false;
    end

end;

procedure ExportSubModel;stdcall;
var
  SaveDialog1:TSaveDialog;
  i_ret:smallint;
  WorldCube:CDI_3DWORLD_CUBE;
  line_bufferextents:CDI_3DWORLD_CUBE;
  object1:CDI_OSL_OBJECT;
  text_buffer:CDI_TEXT_BUFFER;
  line_buffer:CDI_LINESET_BUFFER;
  circle_buffer:CDI_CIRCLE_BUFFER;
  arc_buffer:CDI_ARC_BUFFER;
  line3d_buffer:CDI_3DLINESET_BUFFER;
  face_buffer:CDI_3DFACE_BUFFER;
  symbol_buffer:CDI_SYM_GRP_BUFFER;
  symbol3d_buffer:CDI_3DSYM_GRP_BUFFER;
  seg_id:CDI_SEGID;
  pnt1:CDI_DDD_POINT;
  pnt2:CDI_DDD_POINT;
  obj_type:smallint;
  layer:smallint;
  uniqueId:longint;
  numb:smallint;
  vertex:smallint;
  lvertex:smallint;
  op:smallint;
  textdata:string;
  BlockName:string;
  textlen:smallint;
  lname:string;
  count:longint;

  DatabaseFileName:string;
  ADODataSet1:TADODataset;
  ADODataSet2:TADODataset;
  ADODataSet3:TADODataset;
  ADODataSet4:TADODataset;
  ADOConnection1:TADOConnection;
  VertexArray:array of variant;
  ConnString:string;
  VertexString:string;
  submodelname:string;
  revision:string;
  CoordData:string;
  xp:array[0..200] of WORLD;
  yp:array[0..200] of WORLD;
  zp:array[0..200] of WORLD;
  l_osl:longint;
  textcount:smallint;
  object2:CDI_OSL_OBJECT;
  obj_type2:smallint;
  layer2:smallint;
  area:double;
  description:string;
  BuildingHeight:double;
  BuildingColor:longint;
  notClockwise:boolean;
  extrude:boolean;

begin

  submodelname:='owners';
  description:='building ownership';
  revision:='00';
  extrude:=true;

  submodelname:='terrain';
  description:='terrain model';
  revision:='00';

  SaveDialog1:=TSaveDialog.create(nil);
  SaveDialog1.DefaultExt:='vps';
  SaveDialog1.FileName:='*.vps';
  SaveDialog1.Execute;
  if (SaveDialog1.FileName<>'') or (SaveDialog1.FileName<>'*.vps') then
    begin
      DatabaseFileName:=SaveDialog1.FileName;
    end
  else
    begin
      SaveDialog1.Free;
      exit;
    end;

  WorldCube.ext_x1:=999999999;
  WorldCube.ext_y1:=999999999;
  WorldCube.ext_z1:=999999999;
  WorldCube.ext_x2:=-999999999;
  WorldCube.ext_y2:=-999999999;
  WorldCube.ext_z2:=-999999999;

  ConnString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+
    DatabaseFileName +';Mode=ReadWrite|Share Deny None;Persist Security Info=False';

  ADOConnection1:=TADOConnection.create(nil);
  with ADOConnection1 do begin
    ConnectionString:=ConnString;
    ADOConnection1.Open;
  end;

  //ADOConnection1.execute('DELETE * FROM pEntities');
  ADOConnection1.execute('DELETE * FROM pEntities AS A WHERE EXISTS (SELECT * FROM pObjects WHERE A.AutoObjectNumber = pObjects.AutoObjectNumber AND pObjects.Sub_Mod='''+submodelname+''')');
  ADOConnection1.execute('DELETE * FROM [pObjects] WHERE [sub_mod]='''+submodelname+'''');
  ADOConnection1.execute('DELETE * FROM [pLayers] WHERE [sub_mod]='''+submodelname+'''');
  ADOConnection1.execute('DELETE * FROM [pSubModels] WHERE [sub_mod]='''+submodelname+'''');

  ADOConnection1.Close;
  ADOConnection1.Free;

  count:=CdiOslCount(0);
  if (count=0) then
    begin
      SaveDialog1.Free;
      exit;
    end;

  SaveSubmodelLayers(submodelname, revision, ConnString);

  ADODataSet2:=TADODataset.create(nil);
  with ADODataSet2 do begin
  ADODataSet2.CommandText:='SELECT * FROM [pObjects]';
    ConnectionString:=ConnString;
    Open;
  end;

  ADODataSet3:=TADODataset.create(nil);
  with ADODataSet3 do begin
  ADODataSet3.CommandText:='SELECT * FROM [pEntities]';
    ConnectionString:=ConnString;
    Open;
  end;

  for numb:=0 to count-1 do
    begin

      i_ret:=CdiOslItem(0, numb, object1);
      i_ret:=CdiObjType(object1.sid, obj_type, layer);
      uniqueId:=0;

      case obj_type of
        CDI_TEXT:
          begin
            i_ret:=CdiObjInfo(object1.sid, @text_buffer);
            textlen:=text_buffer.length;
            i_ret:=CdiGetUniqueId(object1.sid, uniqueId);
            lname:=LayerName(text_buffer.layer);
            line_bufferextents.ext_x1:=text_buffer.extents.left;
            line_bufferextents.ext_x2:=text_buffer.extents.right;
            line_bufferextents.ext_y1:=text_buffer.extents.bottom;
            line_bufferextents.ext_y2:=text_buffer.extents.top;
            line_bufferextents.ext_z1:=0;
            line_bufferextents.ext_z2:=0;

            textdata:=text_buffer.string_buffer;
            VertexArray:=nil;
            with ADODataset2 do
              begin
                Append;
                FieldValues['sub_mod']:=submodelname;
                FieldValues['layer']:=LayerName(text_buffer.layer);
                FieldValues['rev']:=revision;
                FieldValues['grp_owner']:=inttostr(uniqueId);
                FieldValues['obj_type']:='D';
                FieldValues['xmin']:=DimtoSingle(line_bufferextents.ext_x1);
                FieldValues['ymin']:=DimtoSingle(line_bufferextents.ext_y1);
                FieldValues['zmin']:=DimtoSingle(line_bufferextents.ext_z1);
                FieldValues['xmax']:=DimtoSingle(line_bufferextents.ext_x2);
                FieldValues['ymax']:=DimtoSingle(line_bufferextents.ext_y2);
                FieldValues['zmax']:=DimtoSingle(line_bufferextents.ext_z2);
                Post;
              end;
              ADODataset2.Last;

              pnt1.x:=text_buffer.position.x;
              pnt1.y:=text_buffer.position.y;
              pnt1.z:=0;

              with ADODataset3 do
                begin
                  Append;
                  FieldValues['AutoObjectNumber']:=ADODataset2.FieldValues['AutoObjectNumber'];
                  FieldValues['Entity_Handle']:=inttostr(uniqueId);
                  FieldValues['Orig_Prim_type']:='TEXT';
                  FieldValues['color']:=ColorValue(text_buffer.color);
                  FieldValues['mark']:='-';
                  FieldValues['mat_prop_Id']:='-';
                  FieldValues['geom_type']:='TEXT';
                  FieldValues['data']:='1'+chr(9)+
                                  floattostr(text_buffer.rotation)+chr(9)+
                                  '1.0'+chr(9)+
                                  floattostr(DimtoSingle(text_buffer.height))+chr(9)+
                                  'F'+chr(9)+
                                  'CADTEXT'+chr(9)+
                                  floattostr(DimtoSingle(pnt1.x))+chr(9)+
                                  floattostr(DimtoSingle(pnt1.y))+chr(9)+
                                  floattostr(DimtoSingle(pnt1.z))+chr(9)+
                                  textdata+chr(9);

                  Post;
                end;

          end;

        CDI_LINESET:
          begin
            BuildingHeight:=0.0;
            i_ret:=CdiObjInfo(object1.sid, @line_buffer);
            i_ret:=CdiEditDraw(object1.sid, 1);
            i_ret:=CdiGetUniqueId(object1.sid, uniqueId);

            lname:=LayerName(line_buffer.layer);
            line_bufferextents.ext_x1:=line_buffer.extents.left;
            line_bufferextents.ext_x2:=line_buffer.extents.right;
            line_bufferextents.ext_y1:=line_buffer.extents.bottom;
            line_bufferextents.ext_y2:=line_buffer.extents.top;
            line_bufferextents.ext_z1:=0;
            line_bufferextents.ext_z2:=0;

            CalculateMax3DExtents(line_bufferextents, WorldCube);
            lvertex:=line_buffer.number_of_elements;

            For vertex:= 0 To lvertex do
              begin
                i_ret:= CdiObjInfoxy(pnt1, op, vertex, line_buffer);
                xp[vertex]:= pnt1.x;
                yp[vertex]:= pnt1.y;
                zp[vertex]:= 0;
              end;
            notClockwise:=Clockwise(xp,yp,lvertex);

            If notClockwise=false then
              begin
                For vertex:=lvertex downto 0 do
                  begin
                    i_ret:= CdiObjInfoxy(pnt1, op, vertex, line_buffer);
                    xp[lvertex-vertex]:= pnt1.x;
                    yp[lvertex-vertex]:= pnt1.y;
                    zp[lvertex-vertex]:= 0;
                  end;
              end;

            textdata:=inttostr(uniqueId);
            l_osl:= CdiCreateLocalOsl(CDI_OBJECT_COLOR, 0, CDI_REDRAW_OSL);
            i_ret:= CdiSelectbyPolygon(l_osl, '', CDI_PARTIAL_FENCE, CDI_TRUE, CDI_TRUE, @xp, @yp, lvertex, CDI_SEL2D_TEXT);
            If (i_ret=CDI_ABORT) Or (i_ret=CDI_ERROR) Then
              begin
              end;

              if CdiOslCount(l_osl)>0 then
                begin
                  for textcount:=0 to CdiOslCount(l_osl)-1 do
                    begin
                      i_ret:=CdiOslItem(l_osl, textcount, object2);
                      i_ret:=CdiGetUniqueId(object2.sid, uniqueId);
                      i_ret:=CdiObjType(object2.sid, obj_type2, layer2);

                      if (obj_type2=CDI_TEXT) then
                        begin
                          i_ret:=CdiObjInfo(object2.sid, @text_buffer);
                          //textpnt:=text_buffer.position;
                          textlen:=text_buffer.length;
                          textdata:=text_buffer.string_buffer;

                          if extrude=true then
                            begin
                              ADODataSet4:=TADODataset.create(nil);
                              with ADODataSet4 do begin
                               ADODataSet4.CommandText:='SELECT * FROM [building information] WHERE [lot name]='''+textdata+'''';
                                ConnectionString:=ConnString;
                                Open;
                                BuildingHeight:=0.0;
                                  if FieldValues['height']<>null then
                                   begin
                                      BuildingHeight:=FieldValues['height'];
                                      BuildingColor:=FieldValues['color'];
                                   end;
                              end; //with

                              ADODataSet4.Close;
                              ADODataSet4.Free;
                            end; //extrude=true

                        end; //(obj_type2=CDI_TEXT)

                    end ; //textcount

                end; //CdiOslCount(l_osl)>0

            i_ret:=CdiFreeLocalOsl(l_osl);

            VertexString:=inttostr(lvertex*3*2);
            For vertex:= 1 To lvertex do
              begin
                VertexString:=VertexString+chr(9)+FormatDimString(xp[vertex-1])+chr(9)+FormatDimString(yp[vertex-1])+chr(9)+FormatDimString(zp[vertex-1]);
                VertexString:=VertexString+chr(9)+FormatDimString(xp[vertex])+chr(9)+FormatDimString(yp[vertex])+chr(9)+FormatDimString(zp[vertex]);
              end; //vertex


              with ADODataset2 do
                begin
                  Append;
                  FieldValues['sub_mod']:=submodelname;
                  FieldValues['layer']:=LayerName(line_buffer.layer);
                  FieldValues['rev']:=revision;
                  FieldValues['grp_owner']:=textdata;
                  FieldValues['obj_type']:='D';
                  FieldValues['xmin']:=DimtoSingle(line_bufferextents.ext_x1);
                  FieldValues['ymin']:=DimtoSingle(line_bufferextents.ext_y1);
                  FieldValues['zmin']:=DimtoSingle(line_bufferextents.ext_z1);
                  FieldValues['xmax']:=DimtoSingle(line_bufferextents.ext_x2);
                  FieldValues['ymax']:=DimtoSingle(line_bufferextents.ext_y2);
                  FieldValues['zmax']:=DimtoSingle(line_bufferextents.ext_z2);
                  Post;
                end;
              ADODataset2.Last;

              if extrude=false then
                begin
                  with ADODataset3 do
                    begin
                      Append;
                      FieldValues['AutoObjectNumber']:=ADODataset2.FieldValues['AutoObjectNumber'];
                      FieldValues['Entity_Handle']:=inttostr(uniqueId);
                      FieldValues['Orig_Prim_type']:='POLYLINE';
                      FieldValues['color']:=ColorValue(line_buffer.color);
                      FieldValues['mark']:='-';
                      FieldValues['mat_prop_Id']:='-';
                      FieldValues['geom_type']:='PLINE';
                      FieldValues['data']:=VertexString;
                      Post;
                    end;
                end;
                
                if BuildingHeight>0 then
                  begin
                    For vertex:= 0 To lvertex-1 do
                      begin
                        VertexString:='12';
                        VertexString:=VertexString+chr(9)+FormatDimString(xp[vertex])+chr(9)+FormatDimString(yp[vertex])+chr(9)+floattostr(0.0);
                        VertexString:=VertexString+chr(9)+FormatDimString(xp[vertex])+chr(9)+FormatDimString(yp[vertex])+chr(9)+floattostr(BuildingHeight);
                        VertexString:=VertexString+chr(9)+FormatDimString(xp[vertex+1])+chr(9)+FormatDimString(yp[vertex+1])+chr(9)+floattostr(BuildingHeight);
                        VertexString:=VertexString+chr(9)+FormatDimString(xp[vertex+1])+chr(9)+FormatDimString(yp[vertex+1])+chr(9)+floattostr(0.0);

                        with ADODataset3 do
                          begin
                            Append;
                            FieldValues['AutoObjectNumber']:=ADODataset2.FieldValues['AutoObjectNumber'];
                            FieldValues['Entity_Handle']:=inttostr(vertex+1);
                            FieldValues['Orig_Prim_type']:='FACE';
                            FieldValues['color']:=BuildingColor;
                            FieldValues['mark']:='-';
                            FieldValues['mat_prop_Id']:='-';
                            FieldValues['geom_type']:='MFACES';
                            FieldValues['data']:=VertexString;
                            Post;
                          end;
                      end;
                   end; //BuildingHeight>0

                if (lvertex=4) and (extrude=true) then
                  begin
                    VertexString:='24';
                    For vertex:= 0 To 4 do
                      begin
                        VertexString:=VertexString+chr(9)+FormatDimString(xp[vertex])+chr(9)+FormatDimString(yp[vertex])+chr(9)+floattostr(BuildingHeight);
                        VertexString:=VertexString+chr(9)+FormatDimString(xp[vertex+1])+chr(9)+FormatDimString(yp[vertex+1])+chr(9)+floattostr(BuildingHeight);
                      end; //vertex
                    with ADODataset3 do
                      begin
                        Append;
                        FieldValues['AutoObjectNumber']:=ADODataset2.FieldValues['AutoObjectNumber'];
                        FieldValues['Entity_Handle']:=inttostr(0);
                        FieldValues['Orig_Prim_type']:='FACE';
                        FieldValues['color']:=BuildingColor;
                        FieldValues['mark']:='-';
                        FieldValues['mat_prop_Id']:='-';
                        FieldValues['geom_type']:='MFACES';
                        FieldValues['data']:=VertexString;
                        Post;
                      end;
                  end; //lvertex=3

{
                //calculate area
                CdiArea(@xp, @yp, lvertex, area);
              with ADODataset3 do
                begin
                  Append;
                  FieldValues['AutoObjectNumber']:=ADODataset2.FieldValues['AutoObjectNumber'];
                  FieldValues['Entity_Handle']:=inttostr(uniqueId);
                  FieldValues['Orig_Prim_type']:='ATTRIBUTE';
                  FieldValues['color']:=ColorValue(line_buffer.color);
                  FieldValues['mark']:='-';
                  FieldValues['mat_prop_Id']:='-';
                  FieldValues['geom_type']:='AREA';
                  FieldValues['data']:=FormatAreaString(area);
                  Post;
                end;
}
                i_ret:=CdiEditDraw(object1.sid, 2);
          end;

        CDI_3DLINESET:
          begin
            i_ret:=CdiObjInfo(object1.sid, @line3d_buffer);
            i_ret:=CdiGetUniqueId(object1.sid, uniqueId);
            lname:=LayerName(line3d_buffer.layer);
            CalculateMax3DExtents(line3d_buffer.extents, WorldCube);
            lvertex:=line3d_buffer.number_of_elements;
            VertexString:=inttostr(lvertex*3*2);
            For vertex:= 1 To lvertex do
              begin
                i_ret:= CdiObjInfoxyz(pnt1, op, vertex-1, line3d_buffer);
                i_ret:= CdiObjInfoxyz(pnt2, op, vertex, line3d_buffer);
                VertexString:=VertexString+chr(9)+FormatDimString(pnt1.x)+chr(9)+FormatDimString(pnt1.y)+chr(9)+FormatDimString(pnt1.z);
                VertexString:=VertexString+chr(9)+FormatDimString(pnt2.x)+chr(9)+FormatDimString(pnt2.y)+chr(9)+FormatDimString(pnt2.z);
              end; //vertex

              with ADODataset2 do
                begin
                  Append;
                  FieldValues['sub_mod']:=submodelname;
                  FieldValues['layer']:=LayerName(line3d_buffer.layer);
                  FieldValues['rev']:=revision;
                  FieldValues['grp_owner']:=inttostr(uniqueId);
                  FieldValues['obj_type']:='D';
                  FieldValues['xmin']:=DimtoSingle(line3d_buffer.extents.ext_x1);
                  FieldValues['ymin']:=DimtoSingle(line3d_buffer.extents.ext_y1);
                  FieldValues['zmin']:=DimtoSingle(line3d_buffer.extents.ext_z1);
                  FieldValues['xmax']:=DimtoSingle(line3d_buffer.extents.ext_x2);
                  FieldValues['ymax']:=DimtoSingle(line3d_buffer.extents.ext_y2);
                  FieldValues['zmax']:=DimtoSingle(line3d_buffer.extents.ext_z2);
                  Post;
                end;
              ADODataset2.Last;
              with ADODataset3 do
                begin
                  Append;
                  FieldValues['AutoObjectNumber']:=ADODataset2.FieldValues['AutoObjectNumber'];
                  FieldValues['Entity_Handle']:=inttostr(uniqueId);
                  FieldValues['Orig_Prim_type']:='POLYLINE';
                  FieldValues['color']:=ColorValue(line3d_buffer.color);
                  FieldValues['mark']:='-';
                  FieldValues['mat_prop_Id']:='-';
                  FieldValues['geom_type']:='PLINE';
                  FieldValues['data']:=VertexString;
                  Post;
                end;

          end;

        CDI_3DFACE:
          begin
            i_ret:=CdiObjInfo(object1.sid, @face_buffer);
            i_ret:=CdiGetUniqueId(object1.sid, uniqueId);
            lname:=LayerName(face_buffer.layer);
            CalculateMax3DExtents(face_buffer.extents, WorldCube);
            lvertex:=face_buffer.vertex_count;
            if (lvertex>2) and (lvertex<5) then
              begin

                VertexString:='12';
                for vertex:= 1 To lvertex do
                  begin
                    i_ret := CdiGetFacexyz(object1.sid, vertex-1, pnt1, op);
                    VertexString:=VertexString+chr(9)+FormatDimString(pnt1.x)+chr(9)+FormatDimString(pnt1.y)+chr(9)+FormatDimString(pnt1.z);
                  end; //vertex

                if lvertex<4 then
                  begin
                    VertexString:=VertexString+chr(9)+FormatDimString(pnt1.x)+chr(9)+FormatDimString(pnt1.y)+chr(9)+FormatDimString(pnt1.z);
                  end;
                //;
                with ADODataset2 do
                  begin
                    Append;
                    FieldValues['sub_mod']:=submodelname;
                    FieldValues['layer']:=LayerName(face_buffer.layer);
                    FieldValues['rev']:=revision;
                    FieldValues['grp_owner']:=inttostr(uniqueId);
                    FieldValues['obj_type']:='D';
                    FieldValues['xmin']:=DimtoSingle(face_buffer.extents.ext_x1);
                    FieldValues['ymin']:=DimtoSingle(face_buffer.extents.ext_y1);
                    FieldValues['zmin']:=DimtoSingle(face_buffer.extents.ext_z1);
                    FieldValues['xmax']:=DimtoSingle(face_buffer.extents.ext_x2);
                    FieldValues['ymax']:=DimtoSingle(face_buffer.extents.ext_y2);
                    FieldValues['zmax']:=DimtoSingle(face_buffer.extents.ext_z2);
                    Post;
                  end;
                ADODataset2.Last;
                with ADODataset3 do
                  begin
                    Append;
                    FieldValues['AutoObjectNumber']:=ADODataset2.FieldValues['AutoObjectNumber'];
                    FieldValues['Entity_Handle']:=inttostr(uniqueId);
                    FieldValues['Orig_Prim_type']:='FACE';
                    FieldValues['color']:=ColorValue(face_buffer.color);
                    FieldValues['mark']:='-';
                    FieldValues['mat_prop_Id']:='-';
                    FieldValues['geom_type']:='MFACES';
                    FieldValues['data']:=VertexString;
                    Post;
                  end;
            end; //(lvertex>2) and (lvertex<5)

          end;

        CDI_3DGROUP:
          begin
            i_ret:=CdiObjInfo(object1.sid, @symbol3d_buffer);
            i_ret:=CdiGetUniqueId(object1.sid, uniqueId);
            CalculateMax3DExtents(symbol3d_buffer.extents, WorldCube);
            lname:=LayerName(symbol3d_buffer.layer);
            VertexString:='7'+chr(9)+
            BlockName+chr(9)+
            floattostr(symbol3d_buffer.scale_x)+chr(9)+
            floattostr(symbol3d_buffer.scale_y)+chr(9)+
            floattostr(symbol3d_buffer.scale_z)+chr(9)+
            floattostr(symbol3d_buffer.rotation_x)+chr(9)+
            floattostr(symbol3d_buffer.rotation_y)+chr(9)+
            floattostr(symbol3d_buffer.rotation_z);

            with ADODataset2 do
              begin
                Append;
                FieldValues['sub_mod']:=submodelname;
                FieldValues['layer']:=LayerName(symbol3d_buffer.layer);
                FieldValues['rev']:=revision;
                FieldValues['grp_owner']:='GROUP-'+inttostr(uniqueId);
                FieldValues['obj_type']:='D';
                FieldValues['xmin']:=DimtoSingle(symbol3d_buffer.extents.ext_x1);
                FieldValues['ymin']:=DimtoSingle(symbol3d_buffer.extents.ext_y1);
                FieldValues['zmin']:=DimtoSingle(symbol3d_buffer.extents.ext_z1);
                FieldValues['xmax']:=DimtoSingle(symbol3d_buffer.extents.ext_x2);
                FieldValues['ymax']:=DimtoSingle(symbol3d_buffer.extents.ext_y2);
                FieldValues['zmax']:=DimtoSingle(symbol3d_buffer.extents.ext_z2);
                Post;
              end;
            SaveMemberData(symbol3d_buffer.position, symbol3d_buffer.master_sgid, ADODataset2.FieldValues['AutoObjectNumber'], ADODataset3);

          end;

        CDI_3DSYMBOL:
          begin
            i_ret:=CdiObjInfo(object1.sid, @symbol3d_buffer);
            i_ret:=CdiGetUniqueId(object1.sid, uniqueId);
            CalculateMax3DExtents(symbol3d_buffer.extents, WorldCube);
            lname:=LayerName(symbol3d_buffer.layer);
            BlockName:=Copy(symbol3d_buffer.symbol_name,1, pos('.',symbol3d_buffer.symbol_name))+inttostr(uniqueid);
            VertexString:='7'+chr(9)+
            BlockName+chr(9)+
            floattostr(symbol3d_buffer.scale_x)+chr(9)+
            floattostr(symbol3d_buffer.scale_y)+chr(9)+
            floattostr(symbol3d_buffer.scale_z)+chr(9)+
            floattostr(symbol3d_buffer.rotation_x)+chr(9)+
            floattostr(symbol3d_buffer.rotation_y)+chr(9)+
            floattostr(symbol3d_buffer.rotation_z);

            with ADODataset2 do
              begin
                Append;
                FieldValues['sub_mod']:=submodelname;
                FieldValues['layer']:=LayerName(symbol3d_buffer.layer);
                FieldValues['rev']:=revision;
                FieldValues['grp_owner']:=BlockName+inttostr(uniqueId);
                FieldValues['obj_type']:='D';
                FieldValues['xmin']:=DimtoSingle(symbol3d_buffer.extents.ext_x1);
                FieldValues['ymin']:=DimtoSingle(symbol3d_buffer.extents.ext_y1);
                FieldValues['zmin']:=DimtoSingle(symbol3d_buffer.extents.ext_z1);
                FieldValues['xmax']:=DimtoSingle(symbol3d_buffer.extents.ext_x2);
                FieldValues['ymax']:=DimtoSingle(symbol3d_buffer.extents.ext_y2);
                FieldValues['zmax']:=DimtoSingle(symbol3d_buffer.extents.ext_z2);
                Post;
              end;
            SaveMemberData(symbol3d_buffer.position, symbol3d_buffer.master_sgid, ADODataset2.FieldValues['AutoObjectNumber'], ADODataset3);
          end;

        else

      end; // case

    end; //numb

  ADODataSet1:=TADODataset.create(nil);
  with ADODataSet1 do begin
  ADODataSet1.CommandText:='SELECT * FROM [pSubModels]';
    ConnectionString:=ConnString;
    Open;
  end;

  with ADODataset1 do
    begin
      Append;
      ADODataset1.FieldValues['sub_mod']:=submodelname;
      ADODataset1.FieldValues['descr']:=description;
      ADODataset1.FieldValues['status']:='test';
      ADODataset1.FieldValues['rev']:=revision;
      ADODataset1.FieldValues['xmin']:=DimtoSingle(WorldCube.ext_x1);
      ADODataset1.FieldValues['ymin']:=DimtoSingle(WorldCube.ext_y1);
      ADODataset1.FieldValues['zmin']:=DimtoSingle(WorldCube.ext_z1);
      ADODataset1.FieldValues['xmax']:=DimtoSingle(WorldCube.ext_x2);
      ADODataset1.FieldValues['ymax']:=DimtoSingle(WorldCube.ext_y2);
      ADODataset1.FieldValues['zmax']:=DimtoSingle(WorldCube.ext_z2);
      ADODataset1.FieldValues['updated']:=date;
      ADODataset1.FieldValues['owner']:='cadvance';
      Post;
    end;

  SaveDialog1.Free;
//  ADODataSet4.Close;
  ADODataSet3.Close;
  ADODataSet2.Close;
  ADODataSet1.Close;
  ADODataSet1.Free;

end;

procedure SaveSubmodelLayers(const submodel:string; const revision:string ; const ConnString:string);
var
  LayerNumber:smallint;
  lname:string;
  ADODataSet1:TADODataset;

begin

  ADODataSet1:=TADODataset.create(nil);
  with ADODataSet1 do begin
    ADODataSet1.CommandText:='SELECT * FROM [pLayers] WHERE [sub_mod]=''' + submodel+ '''';
    ConnectionString:=ConnString;
    Open;

    for LayerNumber:=1 to 255 do
      begin

        if (LayerVisible(LayerNumber)=CDI_YES) and (LayerHasData(LayerNumber)=CDI_TRUE) then
          begin
            lname:=LayerName(LayerNumber);
            with ADODataset1 do
              begin
                Append;
                ADODataset1.FieldValues['sub_mod']:=submodel;
                ADODataset1.FieldValues['rev']:=revision;
                ADODataset1.FieldValues['layer']:=lname;
                ADODataset1.FieldValues['description']:=lname+' (number: '+inttostr(LayerNumber)+')';
                ADODataset1.FieldValues['locked']:='UNLOCKED';
                ADODataset1.FieldValues['color']:=rgb(127,127,127);
                ADODataset1.FieldValues['alpha']:=1.0;
                ADODataset1.FieldValues['visibility']:='ON';
                Post;
              end;
            end; //(LayerVisible(LayerNumber)=CDI_YES) and (LayerHasData(LayerNumber)=CDI_TRUE)

        end; //LayerNumber

    end; //ADODataset

  ADODataSet1.free;

end;

end.

unit LayerUtilities;

interface

uses
  SysUtils,
  Cdiprocs,
  Cdiprocs3D,
  Cditypes,
  Cdiconst;

  Procedure SelectLast;
  Procedure ViewLayersOn;
  Procedure ViewLayersOff;
  Procedure ViewZoomWindow;
  Procedure ViewZoomIn;
  Procedure ViewZoomOut;
  Procedure ViewLayersActive;
  Procedure ViewLayersAll;
  Procedure ZoomWorldExtents;
  Procedure ZoomCenterIn;
  Procedure ZoomCenterOut;
  Procedure ViewLast;
  Procedure ViewFit;
  Procedure PickLayerActive;
  Procedure PickLayerOff;
  Procedure PickLayerUnselect;
  Procedure layer_unselect(number:smallint);
  Procedure layer_off(number:smallint);
  Procedure layer_on(number:smallint);
  Procedure ViewObjectExtents;
  Procedure ViewOnlyLayersUsed;
  Procedure ViewLayersReverse;
  Procedure ViewVertex;

  Procedure SetActiveLayerUp;
  Procedure SetActiveLayerDown;

  function LayerVisible(const LayerNumber:smallint):smallint;
  function LayerColor(const LayerNumber:smallint):smallint;
  function LayerHasData(const LayerNumber:smallint):smallint;
  function LayerName(const LayerNumber:smallint):string;

implementation


Procedure SelectLast;
var
  i_ret:smallint;
  OslObj:CDI_OSL_OBJECT;

begin

  OslObj.sid:=CdiGetLastId;
  i_ret:=CdiOslAdd(0, OslObj, CDI_TRUE);
  if i_ret=CDI_ERROR then
    begin
      CdiPutInfoMsg('','An error has occured adding to the selection set');
    end;

//  i_ret:=CdiObjDraw(0, CDI_HIGHLIGHT);
//  i_ret:=CdiEditDraw(OslObj.sid, CDI_HIGHLIGHT);

end;

Procedure PickLayerActive;
var
  i_ret:smallint;
  object1:CDI_OSL_OBJECT;
  pnt0:CDI_DDD_POINT;
  l_osl:CDI_SEGID;
  obj_type:smallint;
  layer:smallint;
  cvi_callback:PChar;

begin

  l_osl:=CdiCreateLocalOsl(CDI_OBJECT_COLOR, @cvi_callback, CDI_REDRAW_OSL);

  case CdiGetProgMode of
  CDI_2D_MODE:
    begin
      //snapmode=CdiGetSnapMode()
      i_ret:=CdiSetSnapMode(CDI_NOSNAP);
      i_ret:=CdiSelectbyPoint(l_osl,'Select object on layer to make active', CDI_OBJECT_SELECTION, CDI_FALSE, CDI_TRUE, pnt0, CDI_SEL2D_ALL);
      if i_ret=CDI_ABORT then
        begin
          i_ret:=CdiFreeLocalOsl(l_osl);
          exit;
        end;
      i_ret:=CdiOslItem(l_osl, 0, object1);
      i_ret:=CdiObjType(object1.sid, obj_type, layer);
      //i_ret=CdiSetSnapMode(snapmode)
    end;
  CDI_3D_MODE:
    begin
      //snapmode=CdiGet3DSnapMode()
      i_ret:=CdiSet3DSnapMode(CDI_NOSNAP);
      i_ret:=CdiSelect3DByPoint(l_osl, 'Select object on layer to make active', CDI_OBJECT_SELECTION, CDI_FALSE, CDI_TRUE, pnt0, CDI_SEL3D_ALL);
      if i_ret=CDI_ABORT then
        begin
          i_ret:=CdiFreeLocalOsl(l_osl);
          exit;
        end;

      i_ret:=CdiOslItem(l_osl, 0, object1);
      i_ret:=CdiObjType(object1.sid, obj_type, layer);
      //i_ret=CdiSet3DSnapMode(snapmode);
    end;

  end;

  i_ret:=CdiSetActLayer(layer);
  i_ret:=CdiFreeLocalOsl(l_osl);
  CdiStatusLineUpdate;

end;


Procedure ViewLayersOn;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_LAYERS_ON, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW3_LAYERS_ON, term, menu_hit)
    end;

end;


Procedure ViewLayersOff;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_LAYERS_OFF, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW3_LAYERS_OFF, term, menu_hit)
    end;

end;


Procedure ZoomWorldExtents;
var
  i_ret:smallint;
  extents:CDI_WORLD_RECT;
  nullptr:CDI_DDD_POINT;
  pnt1:CDI_DDD_POINT;
  pnt2:CDI_DDD_POINT;

begin

  i_ret:=CdiGetWorldExtents(Extents);

  pnt1.x:=extents.left;
  pnt1.y:=extents.bottom;
  pnt2.x:=extents.right;
  pnt2.y:=extents.top;

  i_ret:=CdiZoom(CDI_ZOOM_WINDOW, 1, nullptr, pnt1, pnt2);

end;


Procedure ViewZoomWindow;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode()=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_ZOOM_WINDOW, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_ZOOM_WINDOW, term, menu_hit);
    end;

end;


Procedure ViewZoomIn;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode()=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_ZOOM_IN, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_ZOOM_IN, term, menu_hit)
    end;

end;


Procedure ViewZoomOut;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode()=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_ZOOM_OUT, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_ZOOM_OUT, term, menu_hit)
    end;

end;


Procedure ZoomCenterOut;
var
  i_ret:smallint;
  pnts:array[0..2] of CDI_DDD_POINT;

begin

  CdiGetViewCenter(pnts[0]);
  i_ret:=CdiZoom(CDI_ZOOM_BY, 0.75, pnts[0], pnts[1], pnts[2]);

end;


Procedure ZoomCenterIn;
var
  i_ret:smallint;
  pnts:array[0..2] of CDI_DDD_POINT;

begin

  CdiGetViewCenter(pnts[0]);
  i_ret:=CdiZoom(CDI_ZOOM_BY, 1.5, pnts[0], pnts[1], pnts[2]);

end;


Procedure ViewLast;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_LAST, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_LAST, term, menu_hit)
    end;

end;


Procedure ViewFit;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_FIT, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_FIT, term, menu_hit)
    end;

end;


Procedure ViewLayersAll;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_LAYERS_ALL, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW3_LAYERS_ALL, term, menu_hit)
    end;

end;


Procedure ViewLayersActive;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgramVersion>=900 then
    begin
      if CdiGetProgMode=CDI_2D_MODE then
        begin
          i_ret:=CdiNestedMenuEntry(IDM_VIEW_LAYERS_ALL_OFF, term, menu_hit)
        end
      else
        begin
          i_ret:=CdiNestedMenuEntry(IDM_VIEW3_LAYERS_ALL_OFF, term, menu_hit)
        end;
      exit;
    end;


  if CdiGetProgMode=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_LAYERS_ACTIVE, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW3_LAYERS_ACTIVE, term, menu_hit)
    end;

end;


Procedure PickLayerOff;
var
  object1:CDI_OSL_OBJECT;
  pnt0:CDI_DDD_POINT;
  cvi_callback:PChar;
  l_osl:longint;
  obj_type:smallint;
  layer:smallint;
  i_ret:smallint;
  cv_layer_turned_off:smallint;

begin

  l_osl:=CdiCreateLocalOsl(CDI_HIGHLIGHT, @cvi_callback, CDI_REDRAW_OSL);

  case CdiGetProgMode of
    CDI_2D_MODE:
      begin
        i_ret:=CdiSetSnapMode(CDI_NOSNAP);
        i_ret:=CdiSelectbyPoint(l_osl, 'Select object on layer to turn off', CDI_OBJECT_SELECTION, CDI_FALSE, CDI_TRUE, pnt0, CDI_SEL2D_ALL);

        if (i_ret=CDI_ABORT) or (i_ret=CDI_ERROR) then
          begin
            i_ret:=CdiFreeLocalOsl(l_osl);
            exit;
          end; //if

        i_ret:=CdiOslItem(l_osl, 0, object1);
        i_ret:=CdiObjType(object1.sid, obj_type , layer);
      end;

    CDI_3D_MODE:
      begin
        i_ret:=CdiSet3DSnapMode(CDI_NOSNAP);
        i_ret:=CdiSelect3DByPoint(l_osl, 'Select object on layer to turn off', CDI_OBJECT_SELECTION, CDI_FALSE, CDI_TRUE, pnt0, CDI_SEL2D_ALL);
        if (i_ret=CDI_ABORT) or (i_ret=CDI_ERROR) then
          begin
            i_ret:=CdiFreeLocalOsl(l_osl);
            exit;
          end; //if
        i_ret:=CdiOslItem(l_osl, 0, object1);
        i_ret:=CdiObjType(object1.sid, obj_type,layer);
      end;

  end; //select

  if (layer=CdiGetActLayer) then
      begin
        CdiPutInfoMsg('Computer Ventures, Inc.','Active layer cannot be turned off!');
      end
    else
      begin
        layer_off(layer);
        cv_layer_turned_off:=layer;
      end;

  i_ret:=CdiFreeLocalOsl(l_osl);
  CdiRedraw;
  CdiStatusLineUpdate;

end; //Procedure


Procedure PickLayerUnselect;
var
  object1:CDI_OSL_OBJECT;
  pnt0:CDI_DDD_POINT;
  cvi_callback:PChar;
  l_osl:longint;
  obj_type:smallint;
  layer:smallint;
  i_ret:smallint;

begin

  l_osl:=CdiCreateLocalOsl(CDI_HIGHLIGHT, @cvi_callback, CDI_REDRAW_OSL);

  case CdiGetProgMode of
    CDI_2D_MODE:
      begin
        i_ret:=CdiSetSnapMode(CDI_NOSNAP);
        i_ret:=CdiSelectbyPoint(l_osl, 'Select object on layer to turn off', CDI_OBJECT_SELECTION, CDI_FALSE, CDI_TRUE, pnt0, CDI_SEL2D_ALL);

        if (i_ret=CDI_ABORT) or (i_ret=CDI_ERROR) then
          begin
            i_ret:=CdiFreeLocalOsl(l_osl);
            exit;
          end; //if

        i_ret:=CdiOslItem(l_osl, 0, object1);
        i_ret:=CdiObjType(object1.sid, obj_type , layer);
      end;

    CDI_3D_MODE:
      begin
        i_ret:=CdiSet3DSnapMode(CDI_NOSNAP);
        i_ret:=CdiSelect3DByPoint(l_osl, 'Select object on layer to turn off', CDI_OBJECT_SELECTION, CDI_FALSE, CDI_TRUE, pnt0, CDI_SEL2D_ALL);
        if (i_ret=CDI_ABORT) or (i_ret=CDI_ERROR) then
          begin
            i_ret:=CdiFreeLocalOsl(l_osl);
            exit;
          end; //if
        i_ret:=CdiOslItem(l_osl, 0, object1);
        i_ret:=CdiObjType(object1.sid, obj_type,layer);
      end;

  end; //select

  if (layer=CdiGetActLayer) then
      begin
        CdiPutInfoMsg('Computer Ventures, Inc.','Active layer cannot be turned off!');
      end
    else
      begin
        layer_unselect(layer);
      end;

  i_ret:=CdiFreeLocalOsl(l_osl);
  CdiRedraw;
  CdiStatusLineUpdate;

end; //Procedure


Procedure layer_unselect(number:smallint);
var
  layer_data:CDI_LAYER;
  i_ret:smallint;

begin
  i_ret:=CdiGetLayer(layer_data,number);
  layer_data.selectable:=CDI_NO;
  i_ret:=CdiSetLayer(layer_data,number);

end;


Procedure layer_off(number:smallint);
var
  layer_data:CDI_LAYER;
  i_ret:smallint;

begin
  i_ret:=CdiGetLayer(layer_data, number);
  layer_data.visible:=CDI_NO;
  layer_data.selectable:=CDI_NO;
  i_ret:=CdiSetLayer(layer_data, number);
end; //Procedure


Procedure layer_on(number:smallint);
var
  layer_data:CDI_LAYER;
  i_ret:smallint;

begin
  i_ret:=CdiGetLayer(layer_data, number);
  layer_data.visible:=CDI_YES;
  layer_data.selectable:=CDI_YES;
  i_ret:=CdiSetLayer(layer_data, number);
end; //Procedure


Procedure ViewObjectExtents;
var
  object1:CDI_OSL_OBJECT;
  symbol_buffer:CDI_SYM_GRP_BUFFER;
  line_buffer:CDI_LINESET_BUFFER;
  circle_buffer:CDI_CIRCLE_BUFFER;
  arc_buffer:CDI_ARC_BUFFER;
  text_buffer:CDI_TEXT_BUFFER;
  node_buffer:CDI_NODE_BUFFER;
  vertex_id1:smallint;
  vertex_id2:smallint;
  pnt0:CDI_DDD_POINT;
  pnt1:CDI_DDD_POINT;
  pnt2:CDI_DDD_POINT;
  snapmode:smallint;
  l_osl:longint;
  obj_type:smallint;
  layer:smallint;
  i_ret:smallint;

begin

if CdiGetProgMode=CDI_3D_MODE then
  begin
    CdiPutInfoMsg(PChar('Not a 3D mode Function'),'');
    exit;
  end;

if CdiOslCount(0)=0 then
begin
  exit;
end;

i_ret:=CdiOslItem(0, 0, object1);
i_ret:=CdiObjType(object1.sid, obj_type, layer);

case obj_type of
CDI_TEXT:
begin
  i_ret:=CdiObjInfo(object1.sid, @text_buffer);
  pnt1.x:=text_buffer.extents.left;
  pnt1.y:=text_buffer.extents.bottom;
  pnt2.x:=text_buffer.extents.right;
  pnt2.y:=text_buffer.extents.top;
end;

CDI_NODE:
begin
  i_ret:=CdiObjInfo(object1.sid, @node_buffer);
  pnt1.x:=node_buffer.extents.left;
  pnt1.y:=node_buffer.extents.bottom;
  pnt2.x:=node_buffer.extents.right;
  pnt2.y:=node_buffer.extents.top;
end;

CDI_GROUP,CDI_SYMBOL:
begin
  i_ret:=CdiObjInfo(object1.sid, @symbol_buffer);
  pnt1.x:=symbol_buffer.extents.left;
  pnt1.y:=symbol_buffer.extents.bottom;
  pnt2.x:=symbol_buffer.extents.right;
  pnt2.y:=symbol_buffer.extents.top;
end;

CDI_CIRCLE:
begin
  i_ret:=CdiObjInfo(object1.sid, @circle_buffer);
  pnt1.x:=circle_buffer.extents.left;
  pnt1.y:=circle_buffer.extents.bottom;
  pnt2.x:=circle_buffer.extents.right;
  pnt2.y:=circle_buffer.extents.top;
end;

CDI_ARC:
begin
  i_ret:=CdiObjInfo(object1.sid, @arc_buffer);
  pnt1.x:=circle_buffer.extents.left;
  pnt1.y:=circle_buffer.extents.bottom;
  pnt2.x:=circle_buffer.extents.right;
  pnt2.y:=circle_buffer.extents.top;
end;

CDI_LINESET:
begin
  i_ret:=CdiSelectLineSegment(object1.sid, pnt0, vertex_id1, vertex_id2);
  i_ret:=CdiObjInfo(object1.sid, @line_buffer);
  pnt1.x:=line_buffer.extents.left;
  pnt1.y:=line_buffer.extents.bottom;
  pnt2.x:=line_buffer.extents.right;
  pnt2.y:=line_buffer.extents.top;
end;

else
begin
  i_ret:=CdiFreeLocalOsl(l_osl);
  i_ret:=CdiSetSnapMode(snapmode);
  exit;
end;

end;

i_ret:=CdiFreeLocalOsl(l_osl);
//call CdiLineCenter(pnt1, pnt2, pnt0);
i_ret:=CdiZoom(CDI_ZOOM_WINDOW, 1, pnt0, pnt1, pnt2);
i_ret:=CdiSetSnapMode(snapmode);

end;


Procedure ViewOnlyLayersUsed;
var
  layer:smallint;
  i_ret:smallint;
  layer_data:CDI_LAYER;

begin

  for layer:=1 to 255 do
    begin
      i_ret:=CdiGetLayer(layer_data,layer);
      if layer_data.data_present=CDI_TRUE then
        begin
          layer_data.visible:=CDI_YES;
          layer_data.selectable:=CDI_YES;
        end
      else
        begin
          layer_data.visible:=CDI_NO;
          layer_data.selectable:=CDI_NO;
        end;
      i_ret:=CdiSetLayer(layer_data, layer);
    end;
  CdiRedraw;

end;


Procedure ViewLayersReverse;
var
  layer:smallint;
  i_ret:smallint;
  layer_data:CDI_LAYER;

begin

  i_ret:=CdiSetActLayer(255);
  CdiStatusLineUpdate;

  for layer:=1 to 255 do
    begin
      i_ret:=CdiGetLayer(layer_data, layer);

      if CdiGetActLayer<>layer then
        begin
          case layer_data.visible of
            CDI_NO:
              begin
              layer_on(layer);
              end;

            CDI_YES:
              begin
                layer_off(layer);
              end;
           end;
        end;
    end;
  CdiRedraw;

end;


Procedure ViewVertex;
var
  i_ret:smallint;
  term:smallint;
  menu_hit:smallint;

begin

  if CdiGetProgMode()=CDI_2D_MODE then
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW_VERTEX, term, menu_hit)
    end
  else
    begin
      i_ret:=CdiNestedMenuEntry(IDM_VIEW3_VERTEX, term, menu_hit);
    end;

end;


Procedure SetActiveLayerUp;
var
  i_ret:smallint;

begin
if CdiGetActLayer()<255 then
  begin
    i_ret:=CdiSetActLayer(CdiGetActLayer()+1);
    CdiStatusLineUpdate;
  end;

end;


Procedure SetActiveLayerDown;
var
  i_ret:smallint;

begin
if CdiGetActLayer()>1 then
  begin
    i_ret:=CdiSetActLayer(CdiGetActLayer()-1);
    CdiStatusLineUpdate;
  end;

end;


function LayerName(const LayerNumber:smallint):string;
var
  LayerData:CDI_LAYER;
  i_ret:smallint;

begin
  i_ret:=CdiGetLayer(LayerData, LayerNumber);
  result:=LayerData.name;
end;


function LayerColor(const LayerNumber:smallint):smallint;
var
  LayerData:CDI_LAYER;

begin

  CdiGetLayer(LayerData, LayerNumber);
  result:=LayerData.color;

end;


function LayerHasData(const LayerNumber:smallint):smallint;
var
  LayerData:CDI_LAYER;

begin

  CdiGetLayer(LayerData, LayerNumber);
  result:=LayerData.data_present;

end;


function LayerVisible(const LayerNumber:smallint):smallint;
var
  LayerData:CDI_LAYER;

begin

  CdiGetLayer(LayerData, LayerNumber);
  result:=LayerData.visible;

end;

end.