| 1 | #include "machdefs.h" | 
|---|
| 2 | #include <stdio.h> | 
|---|
| 3 | #include <stdlib.h> | 
|---|
| 4 | #include <iostream.h> | 
|---|
| 5 | #include <math.h> | 
|---|
| 6 |  | 
|---|
| 7 | #include <typeinfo> | 
|---|
| 8 |  | 
|---|
| 9 | #include <vector> | 
|---|
| 10 | #include <string> | 
|---|
| 11 |  | 
|---|
| 12 | #include "piacmd.h" | 
|---|
| 13 | #include "nobjmgr.h" | 
|---|
| 14 | #include "pistdimgapp.h" | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | //  Module chargeable ds piapp - Commande de gestion de sortie PS | 
|---|
| 18 |  | 
|---|
| 19 | //  Declaration de la fonction d'activation et de desactivation du module | 
|---|
| 20 | extern "C" { | 
|---|
| 21 | void W2PSModule_init(); | 
|---|
| 22 | void W2PSModule_end(); | 
|---|
| 23 | } | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | //  Une classe commande-executor, permettant d'enregistrer de | 
|---|
| 27 | //  nouvelles commandes a piapp | 
|---|
| 28 | class W2PSModuleExecutor : public CmdExecutor { | 
|---|
| 29 | public: | 
|---|
| 30 | W2PSModuleExecutor(); | 
|---|
| 31 | virtual       ~W2PSModuleExecutor(); | 
|---|
| 32 | // Execute s'occupe de l'execution effective des commandes | 
|---|
| 33 | virtual int   Execute(string& keyw, vector<string>& args, string& toks); | 
|---|
| 34 | protected: | 
|---|
| 35 | string psfilename; | 
|---|
| 36 | PSFile * mpsfile; | 
|---|
| 37 | }; | 
|---|
| 38 |  | 
|---|
| 39 | /* --Methode-- */ | 
|---|
| 40 | W2PSModuleExecutor::W2PSModuleExecutor() | 
|---|
| 41 | { | 
|---|
| 42 |  | 
|---|
| 43 | psfilename = "w2ps.ps"; | 
|---|
| 44 | mpsfile = NULL; | 
|---|
| 45 |  | 
|---|
| 46 | PIACmd * mpiac; | 
|---|
| 47 | NamedObjMgr omg; | 
|---|
| 48 | mpiac = omg.GetImgApp()->CmdInterpreter(); | 
|---|
| 49 |  | 
|---|
| 50 | // On enregistre deux nouvelles commandes | 
|---|
| 51 | string hgrp = "Graphics"; | 
|---|
| 52 | // commande w2pssetfilename | 
|---|
| 53 | string kw = "pssetfilename"; | 
|---|
| 54 | string usage = "pssetfilename: Sets current PostScript file name\n" ; | 
|---|
| 55 | usage += "Usage: pssetfilename FileName "; | 
|---|
| 56 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 57 | // commande w2psgetfilename | 
|---|
| 58 | kw = "psgetfilename"; | 
|---|
| 59 | usage = "psgetfilename: Prints current PostScript file name \n" ; | 
|---|
| 60 | usage += "Usage: psgetfilename "; | 
|---|
| 61 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 62 | // commande w2psclosefile | 
|---|
| 63 | kw = "psclosefile"; | 
|---|
| 64 | usage = "psclosefile: close current postscript file \n" ; | 
|---|
| 65 | usage += "Usage: w2psclosefile "; | 
|---|
| 66 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 67 | // commande w2ps | 
|---|
| 68 | kw = "w2ps"; | 
|---|
| 69 | usage = "w2ps: Current Window To PostScript \n" ; | 
|---|
| 70 | usage += "Usage: w2ps "; | 
|---|
| 71 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 72 | // commande w2eps | 
|---|
| 73 | kw = "w2eps"; | 
|---|
| 74 | usage = "w2eps: Current Window To EncapsulatedPostScript \n" ; | 
|---|
| 75 | usage += "Usage: w2eps EPSFileName"; | 
|---|
| 76 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 77 | // commande imagcmap2eps | 
|---|
| 78 | kw = "imagcmap2ps"; | 
|---|
| 79 | usage = "w2ps: Current Image+ColorMap To PostScript \n" ; | 
|---|
| 80 | usage += "Usage: imagcmap2ps "; | 
|---|
| 81 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 82 | // commande imagcmap2eps | 
|---|
| 83 | kw = "imagcmap2eps"; | 
|---|
| 84 | usage = "imagcmap2eps: Current Image+ColorMap To EncapsulatedPostScript \n" ; | 
|---|
| 85 | usage += "Usage: imagcmap2eps EPSFileName"; | 
|---|
| 86 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | /* --Methode-- */ | 
|---|
| 90 | W2PSModuleExecutor::~W2PSModuleExecutor() | 
|---|
| 91 | { | 
|---|
| 92 | if(mpsfile) delete mpsfile; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | /* --Methode-- */ | 
|---|
| 96 | int W2PSModuleExecutor::Execute(string& kw, vector<string>& tokens, string&) | 
|---|
| 97 | { | 
|---|
| 98 |  | 
|---|
| 99 | NamedObjMgr omg; | 
|---|
| 100 | if (kw == "pssetfilename") { | 
|---|
| 101 | if (tokens.size() < 1) { | 
|---|
| 102 | cout << "Usage: pssetfilename PSFileName" << endl; | 
|---|
| 103 | return(0); | 
|---|
| 104 | } | 
|---|
| 105 | if (mpsfile) delete mpsfile; | 
|---|
| 106 | mpsfile = NULL; | 
|---|
| 107 | psfilename = tokens[0]; | 
|---|
| 108 | } | 
|---|
| 109 | else if (kw == "psgetfilename") { | 
|---|
| 110 | cout << " W2PS: PSFileName= " << psfilename << endl; | 
|---|
| 111 | } | 
|---|
| 112 | else if (kw == "psclosefile") { | 
|---|
| 113 | if (mpsfile) { | 
|---|
| 114 | cout << "psclosefile: Closing file " << psfilename << endl; | 
|---|
| 115 | delete mpsfile; | 
|---|
| 116 | mpsfile = NULL; | 
|---|
| 117 | psfilename = "w2ps.ps"; | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 | else if ( (kw == "w2ps") || (kw == "w2eps") ) { | 
|---|
| 121 | PIStdImgApp*  sapp = omg.GetImgApp(); | 
|---|
| 122 | if (sapp == NULL) { | 
|---|
| 123 | cout << " W2PSModuleExecutor::Execute() No PIStdImgApp !" << endl; | 
|---|
| 124 | return(0); | 
|---|
| 125 | } | 
|---|
| 126 | if (sapp->CurrentWindow() == NULL) { | 
|---|
| 127 | cout << " W2PSModuleExecutor::Execute() No current window !" << endl; | 
|---|
| 128 | return(0); | 
|---|
| 129 | } | 
|---|
| 130 | if (kw == "w2ps") { | 
|---|
| 131 | if (mpsfile == NULL) | 
|---|
| 132 | mpsfile = new PSFile(psfilename.c_str(), PI_Portrait, PI_A4, 2., 2.); | 
|---|
| 133 | sapp->CurrentWindow()->PSPrint(mpsfile,0,0); | 
|---|
| 134 | cout << " w2ps: Current window to PS file " << psfilename << endl; | 
|---|
| 135 | } | 
|---|
| 136 | else if (kw == "w2eps") { | 
|---|
| 137 | if (tokens.size() < 1) { | 
|---|
| 138 | cout << "Usage: w2eps EPSFileName" << endl; | 
|---|
| 139 | return(0); | 
|---|
| 140 | } | 
|---|
| 141 | PSFile epsfile(tokens[0].c_str()); | 
|---|
| 142 | sapp->CurrentWindow()->PSPrint(&epsfile,0,0); | 
|---|
| 143 | cout << " w2eps: Current window to EPS file " << tokens[0] << endl; | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 | else if ( (kw == "imagcmap2ps") || (kw == "imagcmap2eps") ) { | 
|---|
| 147 | PIStdImgApp*  sapp = omg.GetImgApp(); | 
|---|
| 148 | if (sapp == NULL) { | 
|---|
| 149 | cout << " W2PSModuleExecutor::Execute() No PIStdImgApp !" << endl; | 
|---|
| 150 | return(0); | 
|---|
| 151 | } | 
|---|
| 152 | PIImage* curpimg = NULL; | 
|---|
| 153 | if (sapp->CurrentBaseWdg()) | 
|---|
| 154 | curpimg = dynamic_cast<PIImage *>(sapp->CurrentBaseWdg()); | 
|---|
| 155 | if (curpimg == NULL) { | 
|---|
| 156 | cout << " W2PSModuleExecutor::Execute() No current PIImage !" << endl; | 
|---|
| 157 | return(0); | 
|---|
| 158 | } | 
|---|
| 159 | PSFile * cpsf = NULL; | 
|---|
| 160 | PSFile * cepsf = NULL; | 
|---|
| 161 | if (kw == "imagcmap2ps") { | 
|---|
| 162 | if (mpsfile == NULL) | 
|---|
| 163 | mpsfile = new PSFile(psfilename.c_str(), PI_Portrait, PI_A4, 2., 2.); | 
|---|
| 164 | cpsf = mpsfile; | 
|---|
| 165 | cout << " imagcmap2ps: Current image+colormap to PS file " << psfilename << endl; | 
|---|
| 166 | } | 
|---|
| 167 | else if (kw == "imagcmap2eps") { | 
|---|
| 168 | if (tokens.size() < 1) { | 
|---|
| 169 | cout << "Usage: imagcmap2eps EPSFileName" << endl; | 
|---|
| 170 | return(0); | 
|---|
| 171 | } | 
|---|
| 172 | cepsf = new PSFile(tokens[0].c_str()); | 
|---|
| 173 | cpsf = cepsf; | 
|---|
| 174 | cout << " imagcmap2eps: Current image+colormap to EPS file " << tokens[0] << endl; | 
|---|
| 175 | } | 
|---|
| 176 | double sx, sy; | 
|---|
| 177 | sx = curpimg->XSize(); | 
|---|
| 178 | sy = curpimg->YSize() + sapp->CMapVW()->YSize() + 10; | 
|---|
| 179 | cpsf->NewPage(sx, sy, PI_Auto); | 
|---|
| 180 | curpimg->PSPrint(cpsf, -curpimg->XPos(), -curpimg->YPos()); | 
|---|
| 181 | sapp->CMapVW()->PSPrint(cpsf, -(sapp->CMapVW()->XPos()), | 
|---|
| 182 | -(sapp->CMapVW()->YPos())+curpimg->YSize()+10, | 
|---|
| 183 | sx/(double)sapp->CMapVW()->XSize(), 1.); | 
|---|
| 184 |  | 
|---|
| 185 | if (cepsf) delete cepsf; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | return(0); | 
|---|
| 189 |  | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | static W2PSModuleExecutor * piaw2psex = NULL; | 
|---|
| 193 | /* Nouvelle-Fonction */ | 
|---|
| 194 | void W2PSModule_init() | 
|---|
| 195 | { | 
|---|
| 196 | // Fonction d'initialisation du module | 
|---|
| 197 | // Appele par le gestionnaire de modules de piapp (PIACmd::LoadModule()) | 
|---|
| 198 | if (piaw2psex) delete piaw2psex; | 
|---|
| 199 | piaw2psex = new W2PSModuleExecutor; | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | /* Nouvelle-Fonction */ | 
|---|
| 203 | void W2PSModule_end() | 
|---|
| 204 | { | 
|---|
| 205 | // Desactivation du module | 
|---|
| 206 | if (piaw2psex) delete piaw2psex; | 
|---|
| 207 | piaw2psex = NULL; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|