/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/



#include <ProToolkit.h>
#include <ProDrawing.h>
#include <ProArray.h>
#include <UtilMatrix.h>
/*====================================================================*\ 
FUNCTION : UsrListViews()
PURPOSE  : Command to list view info in an information window 
\*====================================================================*/ 
int UsrListViews() 
{
  ProDrawing drawing;
  int n_views, view, sheet;
  ProSolid solid;
  ProMdlName modelName;
  ProMdlExtension modelExtension;
  ProVector outline[2];
  double scale;
  ProDrawingViewDisplay display;
  char *sstyle;
  ProView *views;
  FILE *fp;
  ProName wfname;
  char name[PRO_MDLNAME_SIZE];
  char type[PRO_MDLEXTENSION_SIZE];
  ProPath modelFullPath = { 0 }, objDirPath = { 0 };
  int modelVersion;

  char *fname = "views.txt";

/*--------------------------------------------------------------------*\     
Open a text file to contain the information to be displayed 
\*--------------------------------------------------------------------*/ 
  fp = PTApplsUnicodeFopen(fname, "w");

/*--------------------------------------------------------------------*\     
Get the current drawing 
\*--------------------------------------------------------------------*/ 
  ProMdlCurrentGet((ProMdl*)&drawing);

/*--------------------------------------------------------------------*\     
Collect the views into an array 
\*--------------------------------------------------------------------*/ 
  ProDrawingViewsCollect(drawing, &views);
  ProArraySizeGet(views, &n_views);

  for(view=0;view<n_views;view++)
    { 
/*--------------------------------------------------------------------*\
Get the sheet number for this view 
\*--------------------------------------------------------------------*/ 
      ProDrawingViewSheetGet(drawing, views[view], &sheet);

/*--------------------------------------------------------------------*\ 
Get the name of the solid that the view contains 
\*--------------------------------------------------------------------*/ 
      ProDrawingViewSolidGet(drawing, views[view], &solid);
	  ProMdlMdlnameGet((ProMdl)solid, modelName);
	  ProMdlExtensionGet((ProMdl)solid, modelExtension);
      ProMdlOriginGet((ProMdl)solid, modelFullPath);
	  ProFileMdlnameParse (modelFullPath, objDirPath, NULL, NULL, &modelVersion);

      ProWstringToString(name, modelName);
      ProWstringToString(type, modelExtension);

/*--------------------------------------------------------------------*\ 
Get the outline, scale, and display state 
\*--------------------------------------------------------------------*/
      ProDrawingViewOutlineGet(drawing, views[view], outline);         
      ProDrawingViewScaleGet(drawing, views[view], &scale);         
      ProDrawingViewDisplayGet(drawing, views[view], &display);

/*--------------------------------------------------------------------*\ 
Write the information to the text file 
\*--------------------------------------------------------------------*/ 
      ProTKFprintf(fp,"View %d\n", view+1);
      ProTKFprintf(fp,"    Solid        : %s.%s.%d\n", name, type,
	      modelVersion);

      ProTKFprintf(fp,"    Sheet        : %d\n", sheet);
      ProTKFprintf(fp,"    Lower left   : %0.3f, %0.3f\n",
	      outline[0][0],
	      outline[0][1]);

      ProTKFprintf(fp,"    Upper right  : %0.3f, %0.3f\n",
	      outline[1][0],
	      outline[1][1]);

      ProTKFprintf(fp,"    Scale        : %0.3f\n", scale);
      switch(display.style)
	{         
	case PRO_DISPSTYLE_DEFAULT     : sstyle = "default"; break;
	  
	case PRO_DISPSTYLE_WIREFRAME   : sstyle = "wireframe"; break;

	case PRO_DISPSTYLE_HIDDEN_LINE : sstyle = "hidden line"; break;

	case PRO_DISPSTYLE_NO_HIDDEN   : sstyle = "no hidden";break;

	case PRO_DISPSTYLE_SHADED      : sstyle = "shaded"; break;         
	}

      ProTKFprintf(fp,"    Disp style   : %s\n", sstyle);
    }
  
/*--------------------------------------------------------------------*\     
Close the file, and display it 
\*------------------------------------------------------------------*/   
  fclose(fp);     

  ProStringToWstring(wfname, fname);
  ProInfoWindowDisplay(wfname, NULL, NULL);

  return 0;
}


/*====================================================================*\ 
FUNCTION : UsrSolidSelect()
PURPOSE  : Utility to select a solid using the file browser and retrieve
           it if it is not already in session.
\*====================================================================*/ 

int UsrSolidSelect(
     ProSolid *solid)
{
     ProName title;
     ProLine filter;
     ProPath wfile, wdir, wcurrent_dir;
     char file[PRO_PATH_SIZE], dir[PRO_PATH_SIZE],
           current_dir[PRO_PATH_SIZE];
     ProBoolean different_dir;
     char *slash, *root, *ext;
     ProFamilyMdlName wroot;
     ProMdlType mtype;
     ProError status;

/*--------------------------------------------------------------------*\     
Ask the user to select a prt or asm file 
\*--------------------------------------------------------------------*/ 
    ProStringToWstring(title,"Solid");
    ProStringToWstring(filter,"*.prt,*.asm");

    if(ProFileMdlnameOpen(title, filter, NULL, NULL, NULL, NULL, wfile) !=
         PRO_TK_NO_ERROR)

      return(0);
     ProWstringToString(file, wfile);

/*--------------------------------------------------------------------*\     
Parse out the directory, and see if it is different from the current 
\*--------------------------------------------------------------------*/     
     different_dir = PRO_B_FALSE;

     if(slash = strrchr(file,'/'))
     {
        *slash = '\0';
        strcpy(dir, file);
        strcpy(file, ++slash);
        ProStringToWstring(wdir, dir);
        ProDirectoryCurrentGet(wcurrent_dir);
        ProWstringToString(current_dir, wcurrent_dir);
        if(strcmp(current_dir, dir))
             different_dir = PRO_B_TRUE;
     }

/*--------------------------------------------------------------------*\     
Parse out the file root name and model type 
\*--------------------------------------------------------------------*/ 

     root = strtok(file,".");
     ProStringToWstring(wroot, root);
     ext = strtok(NULL,".");
     if(!strcmp(ext,"prt"))
         mtype = PRO_MDL_PART;

     else if(!strcmp(ext,"asm"))
         mtype = PRO_MDL_ASSEMBLY;

     else
         return(0);

/*--------------------------------------------------------------------*\     
If the solid is already in session, return with it 
\*--------------------------------------------------------------------*/ 
    if(ProMdlnameInit(wroot, (ProMdlfileType)mtype, (ProMdl*)solid) == PRO_TK_NO_ERROR)
         return(1);

/*--------------------------------------------------------------------*\     
Move the to correct directory, and try to retieve the solid 
\*--------------------------------------------------------------------*/ 

    if(different_dir)
         ProDirectoryChange(wdir);

    status = ProMdlnameRetrieve(wroot, (ProMdlfileType)mtype, (ProMdl*)solid);

    if(different_dir)
         ProDirectoryChange(wcurrent_dir);

    if(status != PRO_TK_NO_ERROR)
         return(0);

    return(1);
 }

/*====================================================================*\ 
FUNCTION : UsrCreateSheet() 
PURPOSE  : Create a new drawing sheet with a general, and two
           projected,views of a selected solid 
\*====================================================================*/ 

int UsrCreateSheet()
 {
     ProDrawing drawing;
     int sheet;
     ProSolid solid;
     ProVector pos;
     ProView view, pview;
     ProVector outline[2];
     ProMatrix matrix;

/*--------------------------------------------------------------------*\     
Create a new sheet and make it current 
\*--------------------------------------------------------------------*/ 

     ProMdlCurrentGet((ProMdl)&drawing);
     ProDrawingSheetCreate(drawing, &sheet);
     ProDrawingCurrentSheetSet(drawing, sheet);

/*--------------------------------------------------------------------*\     
Ask the user to select a solid and add it to the drawing 
\*--------------------------------------------------------------------*/     

     if(!UsrSolidSelect(&solid))
         return(0);

     ProDrawingSolidAdd(drawing, solid);

/*--------------------------------------------------------------------*\ 
Create a general view from the Z axis direction 
\*--------------------------------------------------------------------*/ 

     ProUtilMatrixCopy(NULL, matrix);
     pos[0] = 200.0;
     pos[1] = 600.0;
     pos[2] = 0.0;

     ProDrawingGeneralviewCreate(drawing, solid, sheet, PRO_B_FALSE,
                 pos, 0.5, matrix, &view);

/*--------------------------------------------------------------------*\     
Get the position and size of the new view 
\*--------------------------------------------------------------------*/ 

    ProDrawingViewOutlineGet(drawing, view, outline);

/*--------------------------------------------------------------------*\     
Create a projected view to the right of the general view 
\*--------------------------------------------------------------------*/ 

     pos[0] = outline[1][0] + (outline[1][0] - outline[0][0]);
     pos[1] = (outline[0][1] + outline[1][1]) / 2.0;
     pos[2] = 0.0;

     ProDrawingProjectedviewCreate(drawing, view, PRO_B_FALSE, pos,
            &pview);

/*--------------------------------------------------------------------*\     
Create a projected view below the general view 
\*--------------------------------------------------------------------*/ 

     pos[0] = (outline[0][0] + outline[1][0]) / 2.0;
     pos[1] = outline[0][1] - (outline[1][1] - outline[0][1]);

     ProDrawingProjectedviewCreate(drawing, view, PRO_B_FALSE, pos,
            &pview);

    return(1);

 }