// File:	DPrsStd_AISPresentationCommands.cxx
// Created:	Wed Oct  7 11:24:34 1998
// Author:	Denis PASCAL
//		<dp@dingox.paris1.matra-dtv.fr>


#include <DPrsStd.hxx>
#include <Draw.hxx>
#include <Draw_Appli.hxx>
#include <Draw_Interpretor.hxx>
#include <Draw_ColorKind.hxx>
#include <TCollection_AsciiString.hxx>
#include <TDocStd_Document.hxx>
#include <TDF_Label.hxx>
#include <TDF_Data.hxx> 
#include <DDF.hxx>
#include <DDocStd.hxx>
#include <ViewerTest.hxx>


#include <V3d_View.hxx>


// for AIS

#include <TPrsStd_AISPresentation.hxx>
#include <TPrsStd_AISViewer.hxx>
#include <ViewerTest.hxx>
#include <AIS_InteractiveContext.hxx> 
#include <AIS_InteractiveObject.hxx>
#include <AIS_Drawer.hxx> 
#include <ViewerTest.hxx>
#include <ViewerTest_Tool.hxx>
#include <V3d_View.hxx>
#include <TCollection_ExtendedString.hxx>
#include <Quantity_NameOfColor.hxx>
#include <Graphic3d_NameOfMaterial.hxx>

#include <TDataStd_Axis.hxx>
#include <TDataStd_Constraint.hxx>
#include <TDataStd_Geometry.hxx>
#include <TNaming_NamedShape.hxx>
#include <TDataStd_Plane.hxx>
#include <TDataStd_Point.hxx>
//#include <TSketchStd_Geometry.hxx>
//#include <TSketchStd_Edge.hxx>

//=======================================================================
//function : DPrsStd_AISDisplay
//purpose  : DDisplay (DOC,entry, not_update)
//=======================================================================

static Standard_Integer DPrsStd_AISDisplay (Draw_Interpretor& di,
					  Standard_Integer nb, 
					  char** arg) 
{   
       
  Handle(TDocStd_Document) D;
  if (!DDocStd::GetDocument(arg[1],D)) return 1; 
  TDF_Label L;
  if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;
  Handle(TPrsStd_AISPresentation) prs;
  if(!L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) return 1;  
  if (nb == 3) {
    prs->Display(Standard_True);
    TPrsStd_AISViewer::Update(L);
    return 0;
  }
  else {
    prs->Display();
    TPrsStd_AISViewer::Update(L);
    return 0;
  }
  di << "DPrsStd_AISDisplay : Error" << "\n";
  return 1;
}

//=======================================================================
//function : DPrsStd_AISRemove
//purpose  : AISRemove (DOC,entry)
//=======================================================================

static Standard_Integer DPrsStd_AISRemove (Draw_Interpretor& di,
					   Standard_Integer nb, 
					   char** arg) 
{   
   if (nb == 3) {     
     Handle(TDocStd_Document) D;
     if (!DDocStd::GetDocument(arg[1],D)) return 1; 
     TDF_Label L;
     if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;
     Handle(TPrsStd_AISPresentation) P;
     if(!L.FindAttribute(TPrsStd_AISPresentation::GetID(), P)) return 1;
     P->Erase(Standard_True);
     TPrsStd_AISViewer::Update(L);
     return 0;
   }
   di << "DPrsStd_AISRedisplay : Error" << "\n";
   return 1;
}


//=======================================================================
//function : DPrsStd_AISErase
//purpose  : AISErase (DOC,entry)
//=======================================================================

static Standard_Integer DPrsStd_AISErase (Draw_Interpretor& di,
					Standard_Integer nb, 
					char** arg) 
{   
  if (nb == 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1; 
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;
    Handle(TPrsStd_AISPresentation) prs;
    if(!L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) return 1;  
    prs->Erase();
    TPrsStd_AISViewer::Update(L);
    return 0;
  }
  di << "DPrsStd_AISErase : Error" << "\n";
  return 1;
}

//=======================================================================
//function : DPrsStd_AISUpdate
//purpose  : AISUpdate (DOC,entry)
//=======================================================================

static Standard_Integer DPrsStd_AISUpdate (Draw_Interpretor& di,
					 Standard_Integer nb, 
					 char** arg) 
{   
  if (nb == 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1; 
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;
    Handle(TPrsStd_AISPresentation) prs;
    if(!L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) return 1;  
    prs->Update();
    TPrsStd_AISViewer::Update(L);
    return 0;
  }
  di << "DPrsStd_AISUpdate : Error" << "\n";
  return 1;
}


//=======================================================================
//function : DPrsStd_AISSet
//purpose  : AISSet (DOC,entry, id)
//=======================================================================

static Standard_Integer DPrsStd_AISSet (Draw_Interpretor& di,
					Standard_Integer nb, 
					char** arg) 
{   
  if (nb == 4) {        
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1; 
    Standard_GUID guid;
    TCollection_ExtendedString str = arg[3];
#ifdef DEB
    cout << "Inputed parameter > " << str   << endl;
#endif
    if ( str == "A" )  //axis
      guid = TDataStd_Axis::GetID();     //"2a96b601-ec8b-11d0-bee7-080009dc3333" 
    else if( str ==  "C")        //constraint 
      guid = TDataStd_Constraint::GetID();    //"2a96b602-ec8b-11d0-bee7-080009dc3333" 
    else if( str == "NS" )        //namedshape
      guid = TNaming_NamedShape::GetID();     //"c4ef4200-568f-11d1-8940-080009dc3333" 
    else if( str == "G" )        //geometry
      guid = TDataStd_Geometry::GetID();      //"2a96b604-ec8b-11d0-bee7-080009dc3333"
    else if( str == "PL" )        //plane
      guid = TDataStd_Plane::GetID();    //"2a96b60c-ec8b-11d0-bee7-080009dc3333"
    else if( str == "PT" )        //point
      guid = TDataStd_Point::GetID();    //"2a96b60d-ec8b-11d0-bee7-080009dc3333"
//    else if( str == "SG" )        //TSketch_Geometry
//      guid = TSketchStd_Geometry::GetID();    //"b3aac909-5b78-11d1-8940-080009dc3333"
//    else if( str == "E" )        //TSketch_Edge
//      guid = TSketchStd_Edge::GetID();           //"b3aac90a-5b78-11d1-8940-080009dc3333"

    Handle(TPrsStd_AISPresentation) prs= TPrsStd_AISPresentation::Set(L, guid);
#ifdef DEB
    cout << "Driver GUID = ";
    prs->GetDriverGUID().ShallowDump(cout);
    cout << "\n";
#endif
    Standard_Character resS[37];
    guid.ToCString(resS);
    di<<resS;
    return 0; 
  }
  di << "DPrsStd_AISSet : Error" << "\n";
  return 1; 
}

//=======================================================================
//function : DPrsStd_AISDriver
//purpose  : AISDriver (DOC,entry, [ID])
//=======================================================================

static Standard_Integer DPrsStd_AISDriver (Draw_Interpretor& di,
					   Standard_Integer nb, 
					   char** arg) 
{   
  if (nb >= 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1; 

    Standard_GUID guid;
    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {
      if( nb == 3 ) {
	guid = prs->GetDriverGUID();
	Standard_Character str[37];
	guid.ToCString( str );
	di << str ;
	return 0; 
      }
      else {
	TCollection_ExtendedString str = arg[3];
#ifdef DEB
	cout << "Inputed parameter > " << str   << endl;
#endif
	if ( str == "A" )  //axis
	  guid = TDataStd_Axis::GetID();     //"2a96b601-ec8b-11d0-bee7-080009dc3333" 
	else if( str ==  "C")        //constraint 
	  guid = TDataStd_Constraint::GetID();    //"2a96b602-ec8b-11d0-bee7-080009dc3333" 
	else if( str == "NS" )        //namedshape
	  guid = TNaming_NamedShape::GetID();     //"c4ef4200-568f-11d1-8940-080009dc3333" 
	else if( str == "G" )        //geometry
	  guid = TDataStd_Geometry::GetID();      //"2a96b604-ec8b-11d0-bee7-080009dc3333"
	else if( str == "PL" )        //plane
	  guid = TDataStd_Plane::GetID();    //"2a96b60c-ec8b-11d0-bee7-080009dc3333"
	else if( str == "PT" )        //point
	  guid = TDataStd_Point::GetID();    //"2a96b60d-ec8b-11d0-bee7-080009dc3333"
//	else if( str == "SG" )        //TSketch_Geometry
//	  guid = TSketchStd_Geometry::GetID();    //"b3aac909-5b78-11d1-8940-080009dc3333"
//	else if( str == "E" )        //TSketch_Edge
//	  guid = TSketchStd_Edge::GetID();           //"b3aac90a-5b78-11d1-8940-080009dc3333"

        prs->SetDriverGUID(guid);
	Standard_Character resS[37];
	guid.ToCString( resS );
	di << resS ;
        return 0;
      }
    }
  }
  di << "DPrsStd_AISDriver : Error" << "\n";
  return 1;  
}

//=======================================================================
//function : DPrsStd_AISUnset
//purpose  : AISUnset (DOC,entry)
//=======================================================================

static Standard_Integer DPrsStd_AISUnset (Draw_Interpretor& di,
					  Standard_Integer nb, 
					  char** arg) 
{   
  if (nb == 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1; 
    TPrsStd_AISPresentation::Unset(L);
    TPrsStd_AISViewer::Update(L);
    return 0; 
  }
  di << "DPrsStd_AISDriver : Error" << "\n";
  return 1;  
}


//=======================================================================
//function : DPrsStd_AISTransparency 
//purpose  : AISTransparency  (DOC,entry,[real])
//=======================================================================

static Standard_Integer DPrsStd_AISTransparency (Draw_Interpretor& di,
						 Standard_Integer nb, 
						 char** arg) 
{
  if (nb >= 3 ) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;  

    Handle(TPrsStd_AISViewer) viewer;
    if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
      if( nb == 4 ) {
	prs->SetTransparency(atof(arg[3]));
	TPrsStd_AISViewer::Update(L);
      }
      else {
       if (prs->HasOwnTransparency()){ 
         di << "Transparency = " << prs->Transparency() << "\n";
         di<<prs->Transparency();
       }
       else{
         di << "DPrsStd_AISTransparency: Warning : Transparency wasn't set" << "\n";
         di<<(-1);
       }
      }
      return 0;
    }
  }
  di << "DPrsStd_AISTransparency : Error"   << "\n";
  return 1;
}

//=======================================================================
//function : DPrsStd_AISDefaultTransparency
//purpose  : AISDefaultTransparency (DOC,entry)
//=======================================================================

static Standard_Integer DPrsStd_AISDefaultTransparency (Draw_Interpretor& di,
							Standard_Integer nb, 
							char** arg) 
{   
  if (nb == 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;   
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;   

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {    
      prs->UnsetTransparency();
      TPrsStd_AISViewer::Update(L);
      return 0;
    }
  }
  di << "DPrsStd_AISDefaultTransparency : Error"   << "\n";
  return 1;
}

//=======================================================================
//function : DPrsStd_AISColor 
//purpose  : AISColor (DOC,entry,[color])
//=======================================================================

static Standard_Integer DPrsStd_AISColor (Draw_Interpretor& di,
					  Standard_Integer nb, 
					  char** arg) 
{
  if (nb >= 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;  

    Handle(TPrsStd_AISViewer) viewer;
    if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
      if( nb == 4 ) {
	prs->SetColor((Quantity_NameOfColor)atoi(arg[3]));
	TPrsStd_AISViewer::Update(L);
      }
      else
       if (prs->HasOwnColor()){ 
         di << "Color = " << prs->Color() << "\n";
         di<<prs->Color();
       }
       else{
         di << "DPrsStd_AISColor: Warning : Color wasn't set" << "\n";
         di<<(-1);
       }
      return 0; 
    }
  }
  di << "DPrsStd_AISColor : Error"   << "\n";
  return 1;
}

//=======================================================================
//function : DPrsStd_AISDefaultColor 
//purpose  : AISDefaultColor (DOC,entry)
//=======================================================================

static Standard_Integer DPrsStd_AISDefaultColor (Draw_Interpretor& di,
						 Standard_Integer nb, 
						 char** arg) 
{
  if (nb == 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;  

    Handle(TPrsStd_AISViewer) viewer;
    if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
      prs->UnsetColor();
      TPrsStd_AISViewer::Update(L);
      return 0; 
    }
  }
  di << "DPrsStd_AISDefaultColor : Error"   << "\n";
  return 1;
}

//=======================================================================
//function : DPrsStd_AISMaterial
//purpose  : AISMaterial (DOC,entry,[material])
//=======================================================================

static Standard_Integer DPrsStd_AISMaterial (Draw_Interpretor& di,
					     Standard_Integer nb, 
					     char** arg) 
{
  if (nb >= 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;  

    Handle(TPrsStd_AISViewer) viewer;
    if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
      if( nb == 4 ) {
	prs->SetMaterial((Graphic3d_NameOfMaterial)atoi(arg[3]));
	TPrsStd_AISViewer::Update(L);
      }
      else {
       if (prs->HasOwnMaterial()){ 
         di << "Material = " << prs->Material() << "\n";
         di<<prs->Material();
       }
       else{
         di << "DPrsStd_AISMaterial: Warning : Material wasn't set" << "\n";
         di<<(-1);
       }
      }
      return 0;
    }
  }
  di << "DPrsStd_AISMaterial : Error"   << "\n";
  return 1;
}

//=======================================================================
//function : DPrsStd_AISDefaultMaterial
//purpose  : AISDefaultMaterial (DOC,entry)
//=======================================================================

static Standard_Integer DPrsStd_AISDefaultMaterial (Draw_Interpretor& di,
						    Standard_Integer nb, 
						    char** arg) 
{
  if (nb == 3) {     
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;  

    Handle(TPrsStd_AISViewer) viewer;
    if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
      prs->UnsetMaterial();
      TPrsStd_AISViewer::Update(L);
      return 0; 
    }
  }
  di << "DPrsStd_AISDefaultMaterial : Error"   << "\n";
  return 1;
}

//=======================================================================
//function : DPrsStd_AISHasOwnColor 
//purpose  : AISHasOwnColor (DOC,entry)
//return   : Boolean 
//=======================================================================

static Standard_Integer DPrsStd_AISHasOwnColor (Draw_Interpretor& di,
					        Standard_Integer nb, 
					        char** arg) 
{
  if (nb >= 3) {     
    if (nb > 3)
      di << "DPrsStd_AISHasOwnColor : Warning : too many arguments" << "\n";

    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;  

    Handle(TPrsStd_AISViewer) viewer;
    if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
      di<<Standard_Integer(prs->HasOwnColor()); 
      return 0; 
    }

  }
  di << "DPrsStd_AISHasOwnColor : Error"   << "\n";
  return 1;
}


//=======================================================================
//function : DPrsStd_AISHasOwnMaterial 
//purpose  : AISHasOwnMaterial (DOC,entry)
//return   : Boolean 
//=======================================================================

static Standard_Integer DPrsStd_AISHasOwnMaterial (Draw_Interpretor& di,
					           Standard_Integer nb, 
					           char** arg) 
{
  if (nb >= 3) {     
    if (nb > 3)
      di << "DPrsStd_AISHasOwnMaterial : Warning : too many arguments" << "\n";

    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;  

    Handle(TPrsStd_AISViewer) viewer;
    if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
      di<<Standard_Integer(prs->HasOwnMaterial()); 
      return 0; 
    }

  }
  di << "DPrsStd_AISHasOwnMaterial : Error"   << "\n";
  return 1;
}


//=======================================================================
//function : DPrsStd_AISHasOwnTransparency 
//purpose  : AISHasOwnColor (DOC,entry)
//return   : Boolean 
//=======================================================================

static Standard_Integer DPrsStd_AISHasOwnTransparency (Draw_Interpretor& di,
					               Standard_Integer nb, 
					               char** arg) 
{
  if (nb >= 3) {     
    if (nb > 3)
      di << "DPrsStd_AISHasOwnTransparency : Warning : too many arguments" << "\n";

    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(arg[1],D)) return 1;  
    TDF_Label L;
    if (!DDF::FindLabel(D->GetData(),arg[2],L)) return 1;  

    Handle(TPrsStd_AISViewer) viewer;
    if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  

    Handle(TPrsStd_AISPresentation) prs;
    if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
      di<<Standard_Integer(prs->HasOwnTransparency()); 
      return 0; 
    }

  }
  di << "DPrsStd_AISHasOwnTransparency : Error"   << "\n";
  return 1;
}


//=======================================================================
//function : AISPresentationCommands
//purpose  :
//=======================================================================


void DPrsStd::AISPresentationCommands (Draw_Interpretor& theCommands)
{  
  static Standard_Boolean done = Standard_False;
  if (done) return;
  done = Standard_True;
  char* g = "DPrsStd : standard presentation commands" ;  
 
  // standard commands working on AISPresentation

  theCommands.Add ("AISDisplay", 
                   "AISDisplay (DOC, entry, [not_update])",
		   __FILE__, DPrsStd_AISDisplay, g);  

  theCommands.Add ("AISErase", 
                   "AISErase (DOC, entry)",
		   __FILE__, DPrsStd_AISErase, g);    

  theCommands.Add ("AISUpdate", 
                   "AISUpdate (DOC, entry)",
		   __FILE__, DPrsStd_AISUpdate, g);  

  theCommands.Add ("AISSet", 
                   "AISSet (DOC, entry, ID)",
		   __FILE__, DPrsStd_AISSet, g);  

  theCommands.Add ("AISDriver", 
                   "AISDriver (DOC, entry, [ID]) - returns DriverGUID stored in attribute or sets new one",
		   __FILE__, DPrsStd_AISDriver, g);  

  theCommands.Add ("AISUnset", 
                   "AISUnset (DOC, entry)",
		   __FILE__, DPrsStd_AISUnset, g);   

  theCommands.Add ("AISTransparency", 
                   "AISTransparency (DOC, entry, [real])",
		   __FILE__, DPrsStd_AISTransparency, g); 

  theCommands.Add ("AISDefaultTransparency", 
                   "AISDefaultTransparency (DOC, entry)",
		   __FILE__, DPrsStd_AISDefaultTransparency, g);

  theCommands.Add ("AISHasOwnTransparency", 
                   "AISHasOwnTransparency (DOC, entry)  |  AISHasOwnTransparency return Boolean",
		   __FILE__, DPrsStd_AISHasOwnTransparency, g);

  theCommands.Add ("AISDefaultColor", 
                   "AISDefaultColor (DOC, entry)",
		   __FILE__, DPrsStd_AISDefaultColor, g);

  theCommands.Add ("AISColor", 
                   "AISColor (DOC, entry, [color])",
		   __FILE__, DPrsStd_AISColor, g);

  theCommands.Add ("AISHasOwnColor", 
                   "AISHasOwnColor (DOC, entry)  |  AISHasOwnColor return Boolean",
		   __FILE__, DPrsStd_AISHasOwnColor, g);

  theCommands.Add ("AISMaterial", 
                   "AISMaterial (DOC, entry, [material])",
		   __FILE__, DPrsStd_AISMaterial, g); 

  theCommands.Add ("AISDefaultMaterial", 
                   "AISDefaultMaterial (DOC, entry)",
		   __FILE__, DPrsStd_AISDefaultMaterial, g); 

  theCommands.Add ("AISHasOwnMaterial", 
                   "AISHasOwnMaterial (DOC, entry)  |  AISHasOwnMaterial return Boolean",
		   __FILE__, DPrsStd_AISHasOwnMaterial, g);

  theCommands.Add ("AISRemove", 
                   "AISRemove (DOC, entry)",
		   __FILE__, DPrsStd_AISRemove, g);
}
