unit step3D21;

interface
  procedure  LIBRARYENTRY( command: WORD  ); Pascal;
  function   LIBRARYID   : Cardinal;         Pascal;
  function   LIBRARYNAME : PChar;            Pascal;

implementation
uses
  Windows,
  SysUtils,
  LDefin2D,
  LDefin3D,
  ksConstTLB,
  ksAuto,
{$IFDEF __LIGHT_VERSION__}
  klTLB;
{$ELSE}
  ksTLB;
{$ENDIF}
var
  iKompasObject: KompasObject;
  iDocument3D: ksDocument3D;

//------------------------------------------------------------------------------
// /  
//-
procedure GetSetPartName;
var
  iPart: ksPart;
begin
  iPart := ksPart(iDocument3D.GetPart( pTop_Part )); //  
  if iPart <> nil then
  begin
		iKompasObject.ksMessage( Format( '  %s', [iPart.name] ) );
		iPart.name := ''; //part.SetName( "" );
		iPart.Update();
	end;
end;

//------------------------------------------------------------------------------
//     
//-
procedure FixAndStandartComponent;
var
  part: ksPart;
  fix: boolean;
  stand: boolean;
begin
	if iDocument3D.IsDetail then
  begin
		iKompasObject.ksError('    ');
		exit;
  end;
  part := ksPart( iDocument3D.GetPart(0) ); //    
	if part <> nil then
  begin
		//     -       - fixedComponent
		fix := part.fixedComponent;
		//     -       - standardComponent
    stand := part.standardComponent;
		//     -       - fixedComponent
    if fix then
    begin
  		iKompasObject.ksMessage( ' ' );
      part.fixedComponent := false;
    end else
    begin
  		iKompasObject.ksMessage( '  ' );
      part.fixedComponent := true;
    end;
		//     -       - standardComponent
    if stand then
    begin
	  	iKompasObject.ksMessage( ' ' );
  		part.standardComponent := false;
    end else
    begin
  		iKompasObject.ksMessage( ' ' );
  		part.standardComponent := true;
    end;
	end;
end;


//------------------------------------------------------------------------------
//      
//-
procedure GetSetColorProperty;
var
  part: ksPart;
  colorPr: ksColorParam;
begin
  part := ksPart( iDocument3D.GetPart(pTop_Part) ); //  
  if part <> nil then
  begin
		colorPr := ksColorParam( part.ColorParam() );
		if colorPr <> nil then
    begin
			iKompasObject.ksMessage( Format('  = %d    = %g    = %g     = %g    = %g    = %g    = %g',
				[ colorPr.Color, colorPr.Ambient, colorPr.Diffuse, colorPr.Specularity, colorPr.Shininess, colorPr.Transparency, colorPr.Emission ] ) );
			colorPr.Color := 5421504;
			colorPr.Transparency := 0.5;
			colorPr.Ambient := 0.1;
			colorPr.Diffuse := 0.1;
			part.Update();
			iKompasObject.ksMessage( Format('  = %d     = %g    = %g     = %g    = %g    = %g    = %g',
				[ colorPr.Color, colorPr.Ambient, colorPr.Diffuse, colorPr.Specularity, colorPr.Shininess, colorPr.Transparency, colorPr.Emission ] ) );
		end;
  end;
end;

//------------------------------------------------------------------------------
//      
//-
procedure GetSetArrayVariable;
var
  part: ksPart;
  varCol: ksVariableCollection;
  variable: ksVariable;
  count: integer;
  i: integer;
  d: double;
begin
	if iDocument3D.IsDetail then
  begin
		iKompasObject.ksError('    ');
		exit;
  end;
  part := ksPart( iDocument3D.GetPart(0) ); //    
  if part <> nil then
  begin
		//     
		varCol := ksVariableCollection(part.VariableCollection() );
		if varCol <> nil then
    begin
      variable := ksVariable( iKompasObject.GetParamStruct(ko_VariableParam) );
			if variable = nil then
				exit;
      count := varCol.GetCount();
			for i := 0 to count - 1 do
      begin
				variable := ksVariable( varCol.GetByIndex(i) );
				iKompasObject.ksMessage( Format( '  %d     %s     %g    %s',
                     [ i, variable.Name, variable.Value, variable.Note ] ) );
				if i = 0 then
        begin
					variable.Note := 'qwerty';
					d := 0;
					iKompasObject.ksReadDouble( ' ', 10, 0, 100, d );
					variable.Value := d;
				end;
				variable := nil;
			end;

      count := varCol.GetCount();
			for i := 0 to count - 1 do //   
      begin
				variable := ksVariable( varCol.GetByIndex(i) );
				iKompasObject.ksMessage( Format( '  %d     %s     %g    %s',
                     [ i, variable.Name, variable.Value, variable.Note ] ) );
				variable := nil;
			end;
			part.RebuildModel();  //  
		end;
	end;
end;

//------------------------------------------------------------------------------
//        
//-
procedure GetSetPlacmentComponent;
var
  part: ksPart;
  plac: ksPlacement;
  x: double;
  y: double;
  z: double;
begin
	if iDocument3D.IsDetail then
  begin
		iKompasObject.ksError('    ');
		exit;
  end;
  part := ksPart( iDocument3D.GetPart(0) ); //    
  if part <> nil then
  begin
		plac := ksPlacement( part.GetPlacement() );
		if plac <> nil then
    begin
			plac.GetOrigin( x, y, z );
			iKompasObject.ksMessage( Format( 'x = %g   y = %g   z = %g', [ x, y, z ] ) );
			plac.SetOrigin( 20, 20, 20 );
			part.UpdatePlacement();
			part.Update();
		end;
	end;
end;

//------------------------------------------------------------------------------
//   ksEntity        
//-
procedure GetSetEntity;
var
  part: ksPart;
  planeXOY: ksEntity;
begin
  part := ksPart( iDocument3D.GetPart(pTop_Part) ); //  
  if part <> nil then
  begin
		planeXOY := ksEntity( part.GetDefaultEntity(o3d_planeXOY) ); // 1-   XOY
		if planeXOY <> nil then
    begin
			iKompasObject.ksMessage( planeXOY.Name );
			planeXOY.Name :='plane';
			planeXOY.Update();
		end;
	end;
end;

//------------------------------------------------------------------------------
//  
//-
procedure CreateSketch;
var
  part: ksPart;
  planeXOY: ksEntity;
  entity: ksEntity;
  sketch: ksSketchDefinition;
  sketchDoc: ksDocument2D;
begin
  part := ksPart( iDocument3D.GetPart(pTop_Part) ); //  
	if part <> nil then
  begin
		planeXOY := ksEntity( part.GetDefaultEntity(o3d_planeXOY) ); // 1-   XOY
		entity := ksEntity( part.NewEntity(o3d_sketch) );
		if ( planeXOY <> nil ) and ( entity <> nil ) then
    begin
			sketch := ksSketchDefinition( entity.GetDefinition() );
			if sketch <> nil then
      begin
				sketch.SetPlane( planeXOY );
				entity.Create();
				sketchDoc := ksDocument2D( sketch.BeginEdit() );//GetSketchEditor() );
  				sketchDoc.ksLineSeg( 0, 0, 100, 100, 1 );
				sketch.EndEdit();
			end;
		end;
	end;
end;

//------------------------------------------------------------------------------
//   ( )     ksEntityCollection ( IEntityCollection )
//-
procedure GetArraySketch;
var
  part: ksPart;
  entityCollection: ksEntityCollection;
  currentEntity: ksEntity;
  k: integer;
begin
  part := ksPart( iDocument3D.GetPart(pTop_Part) ); //  
  if part <> nil then
  begin
		entityCollection := ksEntityCollection( part.EntityCollection( o3d_sketch ) );
//		currentEntity := ksEntity( nil );//( part.NewEntity(o3d_sketch) );
		if entityCollection <> nil then
    begin
			for k := 0 to entityCollection.GetCount() - 1 do
      begin
				currentEntity := ksEntity( entityCollection.GetByIndex(k) );
				iKompasObject.ksMessage( currentEntity.Name );
				currentEntity := nil;
			end;
		end;
	end;
end;

//------------------------------------------------------------------------------
//       
//-
procedure GetSetUserParamComponent;
var
  part: ksPart;
  par: ksUserParam;
  item: ksLtVariant;
  arr: ksDynamicArray;
  par2: ksUserParam;
  item2: ksLtVariant;
  arr2: ksDynamicArray;
  a: double;
  b: double;
  c: integer;
  d: integer;
begin
	if iDocument3D.IsDetail then
  begin
		iKompasObject.ksError('    ');
		exit;
  end;

  part := ksPart( iDocument3D.GetPart(0) ); //    
	par := ksUserParam( iKompasObject.GetParamStruct(ko_UserParam) );
  item := ksLtVariant( iKompasObject.GetParamStruct(ko_LtVariant) );
	arr := ksDynamicArray( iKompasObject.GetDynamicArray(LTVARIANT_ARR) );
	if ( par = nil ) or ( item = nil ) or ( arr = nil ) or ( part = nil ) then
		exit;

	par.Init();
	par.SetUserArray( arr );
	item.Init();
	  item.doubleVal := 12.12;
	  arr.ksAddArrayItem( -1, item );
	item.Init();
  	item.DoubleVal := 21.21;
		arr.ksAddArrayItem( -1, item );
  item.Init();
	  item.IntVal := 666;
	  arr.ksAddArrayItem( -1, item );
  item.Init();
	  item.IntVal := 999;
	  arr.ksAddArrayItem( -1, item );

	part.SetUserParam(par); //   
	part.Update();

  //   
	iKompasObject.ksMessage( Format('   %d', [ part.GetUserParamSize() ] ) );

	par2 := ksUserParam( iKompasObject.GetParamStruct(ko_UserParam) );
  item2 := ksLtVariant( iKompasObject.GetParamStruct(ko_LtVariant) );
	arr2 := ksDynamicArray( iKompasObject.GetDynamicArray(LTVARIANT_ARR) );
	if ( par2 = nil ) or ( item2 = nil ) or ( arr2 = nil ) then
		exit;

	par2.Init();
	par2.SetUserArray( arr2 );
	item2.Init();
	  item2.DoubleVal := 0;
	  arr2.ksAddArrayItem( -1, item2 );
	item2.Init();
  	item2.DoubleVal := 0;
		arr2.ksAddArrayItem( -1, item2 );
  item2.Init();
	  item2.IntVal := 0;
	  arr2.ksAddArrayItem( -1, item2 );
  item2.Init();
	  item2.IntVal := 0;
	  arr2.ksAddArrayItem( -1, item2 );

	part.GetUserParam( par2 ); //  e 

{	struct
		double a, b;
		int c, d;
	 d;   }
  arr2.ksGetArrayItem( 0, item2 );
	a := item2.DoubleVal;
  arr2.ksGetArrayItem( 1, item2 );
	b := item2.DoubleVal;
  arr2.ksGetArrayItem( 2, item2 );
	c := item2.IntVal;
  arr2.ksGetArrayItem( 3, item2 );
	d := item2.IntVal;
  //     
	iKompasObject.ksMessage( Format('     a = %g   b = %g   c = %d   d = %d', [ a, b, c, d ] ) );
end;

//------------------------------------------------------------------------------
//  
//-
procedure CreateDocument3D;
begin
	iDocument3D := ksDocument3D( iKompasObject.Document3D() );
  if iDocument3D.Create(false, true) then
  begin
    iDocument3D.author := '';                //  
    iDocument3D.comment := '  3D'; //   
		iDocument3D.fileName := 'c:\example.m3d';    //   
    iDocument3D.UpdateDocumentParam();             //   
		iDocument3D.Save();                            //  
		iKompasObject.ksMessage( '    ' );
    iDocument3D.SaveAs( 'c:\example_1.m3d' );     //     

		//  
		iKompasObject.ksMessage (' : ' + iDocument3D.author);
		//   
		iKompasObject.ksMessage( '  : ' + iDocument3D.comment );
    //  
		iKompasObject.ksMessage( ' : ' + iDocument3D.fileName );

		iDocument3D.close(); //  
  end;

end;

//------------------------------------------------------------------------------
//
//-
procedure UseEntityCollection;
var
  part: ksPart;
  count1: Longint;
  count2: Longint;
  count: Longint;
  collect: ksEntityCollection;
  colorPr: ksColorParam;
  ent: ksEntity;
  faceDef: ksFaceDefinition;
  i : integer;
  collect2: ksEntityCollection;
  edgeDef: ksEdgeDefinition;
begin
  part := ksPart( iDocument3D.GetPart(pNew_Part) );       //  
  if part <> nil then
  begin
    count := 0;
    count1 := 0; //   
    count2 := 0; //   
    //  
    collect := ksEntityCollection( part.EntityCollection(o3d_face) );
    if collect <> nil then
    begin
      count := collect.GetCount();
      for i := 0 to count - 1 do begin
        ent := ksEntity( collect.GetByIndex(i) );
        colorPr := ksColorParam( ent.ColorParam );
        //   
        faceDef := ksFaceDefinition( ent.GetDefinition() );
        if faceDef <> nil then
        begin
          if ( faceDef.IsCone() ) or ( faceDef.IsCylinder() ) then //   -
          begin
            count2 := count2 + 1;   //   
          end;

          if faceDef.IsPlanar() then //   -
          begin
            count1 := count1 + 1;   //   
          end;

          colorPr := nil;
          ent.Update(); //  
        end;
      end;
    end;

    //    
    if count = 0 then
      iKompasObject.ksMessage( '    ' )
    else
      iKompasObject.ksMessage(Format(' %d   %d  ', [count2, count1] ));

    count1 := 0;
    count2 := 0;
    //  
    collect2 := ksEntityCollection( part.EntityCollection(o3d_edge) );
    count := collect2.GetCount();
    if ( collect2 <> nil ) and ( count <> 0 ) then
    begin
      for i := 0 to count - 1 do
      begin
        ent := ksEntity( collect2.GetByIndex(i) );
        edgeDef := ksEdgeDefinition( ent.GetDefinition() );
        if edgeDef <> nil then
          if edgeDef.IsStraight() then
            count1 := count1 + 1 //   
          else
            count2 := count2 + 1; //   
      end;
    end;

    //    
    if count = 0 then
      iKompasObject.ksMessage( '    ' )
    else
      iKompasObject.ksMessage( Format( ' %d   %d  ', [ count1, count2 ] ) );
  end;
end;

//------------------------------------------------------------------------------
//        
//-
procedure DocIterator;
var
  doc: ksDocument3D;
  arrDoc: ksDynamicArray;
  item: ksChar255;
  i: integer;
  iter: ksIterator;
  ref: Reference;
  count: Integer;
begin
	doc := ksDocument3D( iKompasObject.Document3D() );
	arrDoc := ksDynamicArray( iKompasObject.GetDynamicArray(CHAR_STR_ARR) ); //      
	//       
  if arrDoc <> nil then
  begin
    iKompasObject.ksChoiceFiles( '*.m3d',' (*.m3d)|*.m3d|  (*.*)|*.*|', arrDoc, false);
    count := arrDoc.ksGetArrayCount();
    if count = 0 then
      exit;
    item := ksChar255( iKompasObject.GetParamStruct(ko_Char255) );
		if item <> nil then
    begin
			//     
      for i := 0 to count - 1 do
				if arrDoc.ksGetArrayItem(i, item) <> 0 then
        begin
					doc := ksDocument3D(iKompasObject.Document3D()); //   
					doc.Open( item.str, false ); //     
					doc := nil; //  
				end;
		end;

		//    
    iKompasObject.ksMessage( ' ' + IntToStr(arrDoc.ksGetArrayCount()) + ' ' );

		//    
		iter := ksIterator( iKompasObject.GetIterator() );
		if iter <> nil then
    begin
      iter.ksCreateIterator(D3_DOCUMENT_OBJ, 0);
      ref := iter.ksMoveIterator('F');  //     
      while bool(ref) do  //     
      begin
        //   
        doc := nil; //  
        doc := ksDocument3D( iKompasObject.ksGet3dDocumentFromRef(ref) );
        if doc <> nil then
        begin
          iKompasObject.ksMessage( ' : ' + doc.author ); //  
          iKompasObject.ksMessage( '  : ' + doc.comment ); //   
          iKompasObject.ksMessage( ' : ' + doc.filename ); //   
        end;
        ref := iter.ksMoveIterator('N');
      end;
      iter.ksDeleteIterator(); //  
		end;
	end;
end;

//------------------------------------------------------------------------------
// LibraryName
//---
function LIBRARYNAME: PChar; pascal;
begin
  Result := '   (  )';
end;

//------------------------------------------------------------------------------
// LibraryId
//---
function LIBRARYID: UINT; pascal;
begin
  Result := 100;
end;

//------------------------------------------------------------------------------
// LibraryEntry
//---
procedure LIBRARYENTRY( command: WORD ); pascal;
begin
  iKompasObject := KompasObject(CreateKompasObject);

  if iKompasObject <> nil then
  begin
    iDocument3D := ksDocument3D(iKompasObject.ActiveDocument3D);

    if ( ( command = 1 ) or ( command = 2 ) ) then
    begin
      case command of
        1 : CreateDocument3D;
        2 : DocIterator;
      end;
    end else
      if (iDocument3D <> nil) then
      begin
        case command of
          3  : UseEntityCollection;       //    
   				4  : GetSetPartName;	          // /  
          5  : FixAndStandartComponent;   //     
          6  : GetSetColorProperty;       //      
          7  : GetSetArrayVariable;    	  //      
          8  : GetSetPlacmentComponent;   //        
          9  : GetSetEntity;              //   ksEntity        
          10 : CreateSketch;              //  
          11 : GetArraySketch;            //   ( )     ksEntityCollection ( IEntityCollection )
          12 : GetSetUserParamComponent;  //       
        end;

        iKompasObject.ksMessageBoxResult;

        iDocument3D := nil;
      end;
    //end;

    iKompasObject := nil;
  end;
end;

end.
