home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / pstoedit.zip / source.zip / pstoedit.2.50 / src / drvdxf.cpp < prev    next >
C/C++ Source or Header  |  1996-11-07  |  14KB  |  443 lines

  1. /* 
  2.    drvDXF.cpp : This file is part of pstoedit 
  3.  
  4.    Copyright (C) 1993,1994,1995,1996 Wolfgang Glunz, Wolfgang.Glunz@zfe.siemens.de
  5.  
  6.     DXF Backend Version 0.9 ( LINEs only, no Text, no color, no linewidth )
  7.     (see #ifdef aslines )
  8.     Carsten Hammer    chammer@post.uni-bielefeld.de
  9.        CATS Gesellschaft fuer technische DV-Anwendungen mbH
  10.     Carl-Zeiss-Strasse 65
  11.     33334 Guetersloh
  12.  
  13.     This program is free software; you can redistribute it and/or modify
  14.     it under the terms of the GNU General Public License as published by
  15.     the Free Software Foundation; either version 2 of the License, or
  16.     (at your option) any later version.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program; if not, write to the Free Software
  25.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27. */
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <iostream.h>
  32.  
  33. #include "drvdxf.h"
  34.  
  35. class DXFColor {
  36. public:
  37. // The SparcCompiler wants to have this public in order to initialize DXFColors
  38. struct rgbcolor {
  39.     unsigned short r;
  40.     unsigned short g;
  41.     unsigned short b;
  42. };
  43.  
  44. private:
  45. static const rgbcolor DXFColors[];
  46. static const unsigned short numberOfColors;
  47. public:
  48. static unsigned int getDXFColor(float r, float g, float b)
  49. {
  50.     float mindist = 1.0f;
  51.     unsigned int best = 0;
  52.     for (unsigned int i = 0 ; i < numberOfColors; i++) {
  53.         const float dxfr = DXFColors[i].r / 255.0f;
  54.         const float dxfg = DXFColors[i].g / 255.0f;
  55.         const float dxfb = DXFColors[i].b / 255.0f;
  56.         const float dr = dxfr - r;
  57.         const float dg = dxfg - g;
  58.         const float db = dxfb - b;
  59.         const float dist = dr * dr + dg * dg + db * db;
  60.         if (dist == 0.0 ) {
  61. //            errf << "Found exact match for " << r << " "
  62. //                << g << " " << b << " " << i  << " " << numberOfColors << endl;
  63.             return i;
  64.         } else if (dist < mindist) {
  65.             best = i;
  66.             mindist = dist;
  67.         }
  68.     }
  69. //    errf << "Found approximation for " << r << " "
  70. //        << g << " " << b << " " << best  << " " << mindist  << " " << numberOfColors << endl;
  71.     return best;
  72. }
  73. };
  74. const DXFColor::rgbcolor DXFColor::DXFColors[] = {
  75.         { 0x00, 0x00, 0x00 },      //0
  76.         { 0xFF, 0x00, 0x00 },
  77.         { 0xFF, 0xFF, 0x00 },
  78.         { 0x00, 0xFF, 0x00 },
  79.         { 0x00, 0xFF, 0xFF },
  80.         { 0x00, 0x00, 0xFF },      //5
  81.         { 0xFF, 0x00, 0xFF },
  82.         { 0xFF, 0xFF, 0xFF },
  83.         { 0x41, 0x41, 0x41 },
  84.         { 0x80, 0x80, 0x80 },
  85.         { 0xFF, 0x00, 0x00 },      //10
  86.         { 0xFF, 0xAA, 0xAA },
  87.         { 0xBD, 0x00, 0x00 },
  88.         { 0xBD, 0x7E, 0x7E },
  89.         { 0x81, 0x00, 0x00 },
  90.         { 0x81, 0x56, 0x56 },      //15
  91.         { 0x68, 0x00, 0x00 },
  92.         { 0x68, 0x45, 0x45 },
  93.         { 0x4F, 0x00, 0x00 },
  94.         { 0x4F, 0x35, 0x35 },
  95.         { 0xFF, 0x3F, 0x00 },      //20
  96.         { 0xFF, 0xBF, 0xAA },
  97.         { 0xBD, 0x2E, 0x00 },
  98.         { 0xBD, 0x8D, 0x7E },
  99.         { 0x81, 0x1F, 0x00 },
  100.         { 0x81, 0x60, 0x56 },      //25
  101.         { 0x68, 0x19, 0x00 },
  102.         { 0x68, 0x4E, 0x45 },
  103.         { 0x4F, 0x13, 0x00 },
  104.         { 0x4F, 0x3B, 0x35 },
  105.         { 0xFF, 0x7F, 0x00 },      //30
  106.         { 0xFF, 0xD4, 0xAA },
  107.         { 0xBD, 0x5E, 0x00 },
  108.         { 0xBD, 0x9D, 0x7E },
  109.         { 0x81, 0x40, 0x00 },
  110.         { 0x81, 0x6B, 0x56 },      //35
  111.         { 0x68, 0x34, 0x00 },
  112.         { 0x68, 0x56, 0x45 },
  113.         { 0x4F, 0x27, 0x00 },
  114.         { 0x4F, 0x42, 0x35 },
  115.         { 0xFF, 0xBF, 0x00 },      //40
  116.         { 0xFF, 0xEA, 0xAA },
  117.         { 0xBD, 0x8D, 0x00 },
  118.         { 0xBD, 0xAD, 0x7E },
  119.         { 0x81, 0x60, 0x00 },
  120.         { 0x81, 0x76, 0x56 },      //45
  121.         { 0x68, 0x4E, 0x00 },
  122.         { 0x68, 0x5F, 0x45 },
  123.         { 0x4F, 0x3B, 0x00 },
  124.         { 0x4F, 0x49, 0x35 },
  125.         { 0xFF, 0xFF, 0x00 },      //50
  126.         { 0xFF, 0xFF, 0xAA },
  127.         { 0xBD, 0xBD, 0x00 },
  128.         { 0xBD, 0xBD, 0x7E },
  129.         { 0x81, 0x81, 0x00 },
  130.         { 0x81, 0x81, 0x56 },      //55
  131.         { 0x68, 0x68, 0x00 },
  132.         { 0x68, 0x68, 0x45 },
  133.         { 0x4F, 0x4F, 0x00 },
  134.         { 0x4F, 0x4F, 0x35 },
  135.         { 0xBF, 0xFF, 0x00 },      //60
  136.         { 0xEA, 0xFF, 0xAA },
  137.         { 0x8D, 0xBD, 0x00 },
  138.         { 0xAD, 0xBD, 0x7E },
  139.         { 0x60, 0x81, 0x00 },
  140.         { 0x76, 0x81, 0x56 },      //65
  141.         { 0x4E, 0x68, 0x00 },
  142.         { 0x5F, 0x68, 0x45 },
  143.         { 0x3B, 0x4F, 0x00 },
  144.         { 0x49, 0x4F, 0x35 },
  145.         { 0x7F, 0xFF, 0x00 },      //70
  146.         { 0xD4, 0xFF, 0xAA },
  147.         { 0x5E, 0xBD, 0x00 },
  148.         { 0x9D, 0xBD, 0x7E },
  149.         { 0x40, 0x81, 0x00 },
  150.         { 0x6B, 0x81, 0x56 },      //75
  151.         { 0x34, 0x68, 0x00 },
  152.         { 0x56, 0x68, 0x45 },
  153.         { 0x27, 0x4F, 0x00 },
  154.         { 0x42, 0x4F, 0x35 },
  155.         { 0x3F, 0xFF, 0x00 },      //80
  156.         { 0xBF, 0xFF, 0xAA },
  157.         { 0x2E, 0xBD, 0x00 },
  158.         { 0x8D, 0xBD, 0x7E },
  159.         { 0x1F, 0x81, 0x00 },
  160.         { 0x60, 0x81, 0x56 },      //85
  161.         { 0x19, 0x68, 0x00 },
  162.         { 0x4E, 0x68, 0x45 },
  163.         { 0x13, 0x4F, 0x00 },
  164.         { 0x3B, 0x4F, 0x35 },
  165.         { 0x00, 0xFF, 0x00 },      //90
  166.         { 0xAA, 0xFF, 0xAA },
  167.         { 0x00, 0xBD, 0x00 },
  168.         { 0x7E, 0xBD, 0x7E },
  169.         { 0x00, 0x81, 0x00 },
  170.         { 0x56, 0x81, 0x56 },      //95
  171.         { 0x00, 0x68, 0x00 },
  172.         { 0x45, 0x68, 0x45 },
  173.         { 0x00, 0x4F, 0x00 },
  174.         { 0x35, 0x4F, 0x35 },
  175.         { 0x00, 0xFF, 0x3F },      //100
  176.         { 0xAA, 0xFF, 0xBF },
  177.         { 0x00, 0xBD, 0x2E },
  178.         { 0x7E, 0xBD, 0x8D },
  179.         { 0x00, 0x81, 0x1F },
  180.         { 0x56, 0x81, 0x60 },      //105
  181.         { 0x00, 0x68, 0x19 },
  182.         { 0x45, 0x68, 0x4E },
  183.         { 0x00, 0x4F, 0x13 },
  184.         { 0x35, 0x4F, 0x3B },
  185.         { 0x00, 0xFF, 0x7F },      //110
  186.         { 0xAA, 0xFF, 0xD4 },
  187.         { 0x00, 0xBD, 0x5E },
  188.         { 0x7E, 0xBD, 0x9D },
  189.         { 0x00, 0x81, 0x40 },
  190.         { 0x56, 0x81, 0x6B },      //115
  191.         { 0x00, 0x68, 0x34 },
  192.         { 0x45, 0x68, 0x56 },
  193.         { 0x00, 0x4F, 0x27 },
  194.         { 0x35, 0x4F, 0x42 },
  195.         { 0x00, 0xFF, 0xBF },      //120
  196.         { 0xAA, 0xFF, 0xEA },
  197.         { 0x00, 0xBD, 0x8D },
  198.         { 0x7E, 0xBD, 0xAD },
  199.         { 0x00, 0x81, 0x60 },
  200.         { 0x56, 0x81, 0x76 },      //125
  201.         { 0x00, 0x68, 0x4E },
  202.         { 0x45, 0x68, 0x5F },
  203.         { 0x00, 0x4F, 0x3B },
  204.         { 0x35, 0x4F, 0x49 },
  205.         { 0x00, 0xFF, 0xFF },      //130
  206.         { 0xAA, 0xFF, 0xFF },
  207.         { 0x00, 0xBD, 0xBD },
  208.         { 0x7E, 0xBD, 0xBD },
  209.         { 0x00, 0x81, 0x81 },
  210.         { 0x56, 0x81, 0x81 },      //135
  211.         { 0x00, 0x68, 0x68 },
  212.         { 0x45, 0x68, 0x68 },
  213.         { 0x00, 0x4F, 0x4F },
  214.         { 0x35, 0x4F, 0x4F },
  215.         { 0x00, 0xBF, 0xFF },      //140
  216.         { 0xAA, 0xEA, 0xFF },
  217.         { 0x00, 0x8D, 0xBD },
  218.         { 0x7E, 0xAD, 0xBD },
  219.         { 0x00, 0x60, 0x81 },
  220.         { 0x56, 0x76, 0x81 },      //145
  221.         { 0x00, 0x4E, 0x68 },
  222.         { 0x45, 0x5F, 0x68 },
  223.         { 0x00, 0x3B, 0x4F },
  224.         { 0x35, 0x49, 0x4F },
  225.         { 0x00, 0x7F, 0xFF },      //150
  226.         { 0xAA, 0xD4, 0xFF },
  227.         { 0x00, 0x5E, 0xBD },
  228.         { 0x7E, 0x9D, 0xBD },
  229.         { 0x00, 0x40, 0x81 },
  230.         { 0x56, 0x6B, 0x81 },      //155
  231.         { 0x00, 0x34, 0x68 },
  232.         { 0x45, 0x56, 0x68 },
  233.         { 0x00, 0x27, 0x4F },
  234.         { 0x35, 0x42, 0x4F },
  235.         { 0x00, 0x3F, 0xFF },      //160
  236.         { 0xAA, 0xBF, 0xFF },
  237.         { 0x00, 0x2E, 0xBD },
  238.         { 0x7E, 0x8D, 0xBD },
  239.         { 0x00, 0x1F, 0x81 },
  240.         { 0x56, 0x60, 0x81 },      //165
  241.         { 0x00, 0x19, 0x68 },
  242.         { 0x45, 0x4E, 0x68 },
  243.         { 0x00, 0x13, 0x4F },
  244.         { 0x35, 0x3B, 0x4F },
  245.         { 0x00, 0x00, 0xFF },      //170
  246.         { 0xAA, 0xAA, 0xFF },
  247.         { 0x00, 0x00, 0xBD },
  248.         { 0x7E, 0x7E, 0xBD },
  249.         { 0x00, 0x00, 0x81 },
  250.         { 0x56, 0x56, 0x81 },      //175
  251.         { 0x00, 0x00, 0x68 },
  252.         { 0x45, 0x45, 0x68 },
  253.         { 0x00, 0x00, 0x4F },
  254.         { 0x35, 0x35, 0x4F },
  255.         { 0x3F, 0x00, 0xFF },      //180
  256.         { 0xBF, 0xAA, 0xFF },
  257.         { 0x2E, 0x00, 0xBD },
  258.         { 0x8D, 0x7E, 0xBD },
  259.         { 0x1F, 0x00, 0x81 },
  260.         { 0x60, 0x56, 0x81 },      //185
  261.         { 0x19, 0x00, 0x68 },
  262.         { 0x4E, 0x45, 0x68 },
  263.         { 0x13, 0x00, 0x4F },
  264.         { 0x3B, 0x35, 0x4F },
  265.         { 0x7F, 0x00, 0xFF },      //190
  266.         { 0xD4, 0xAA, 0xFF },
  267.         { 0x5E, 0x00, 0xBD },
  268.         { 0x9D, 0x7E, 0xBD },
  269.         { 0x40, 0x00, 0x81 },
  270.         { 0x6B, 0x56, 0x81 },      //195
  271.         { 0x34, 0x00, 0x68 },
  272.         { 0x56, 0x45, 0x68 },
  273.         { 0x27, 0x00, 0x4F },
  274.         { 0x42, 0x35, 0x4F },
  275.         { 0xBF, 0x00, 0xFF },      //200
  276.         { 0xEA, 0xAA, 0xFF },
  277.         { 0x8D, 0x00, 0xBD },
  278.         { 0xAD, 0x7E, 0xBD },
  279.         { 0x60, 0x00, 0x81 },
  280.         { 0x76, 0x56, 0x81 },      //205
  281.         { 0x4E, 0x00, 0x68 },
  282.         { 0x5F, 0x45, 0x68 },
  283.         { 0x3B, 0x00, 0x4F },
  284.         { 0x49, 0x35, 0x4F },
  285.         { 0xFF, 0x00, 0xFF },      //210
  286.         { 0xFF, 0xAA, 0xFF },
  287.         { 0xBD, 0x00, 0xBD },
  288.         { 0xBD, 0x7E, 0xBD },
  289.         { 0x81, 0x00, 0x81 },
  290.         { 0x81, 0x56, 0x81 },      //215
  291.         { 0x68, 0x00, 0x68 },
  292.         { 0x68, 0x45, 0x68 },
  293.         { 0x4F, 0x00, 0x4F },
  294.         { 0x4F, 0x35, 0x4F },
  295.         { 0xFF, 0x00, 0xBF },      //220
  296.         { 0xFF, 0xAA, 0xEA },
  297.         { 0xBD, 0x00, 0x8D },
  298.         { 0xBD, 0x7E, 0xAD },
  299.         { 0x81, 0x00, 0x60 },
  300.         { 0x81, 0x56, 0x76 },      //225
  301.         { 0x68, 0x00, 0x4E },
  302.         { 0x68, 0x45, 0x5F },
  303.         { 0x4F, 0x00, 0x3B },
  304.         { 0x4F, 0x35, 0x49 },
  305.         { 0xFF, 0x00, 0x7F },      //230
  306.         { 0xFF, 0xAA, 0xD4 },
  307.         { 0xBD, 0x00, 0x5E },
  308.         { 0xBD, 0x7E, 0x9D },
  309.         { 0x81, 0x00, 0x40 },
  310.         { 0x81, 0x56, 0x6B },      //235
  311.         { 0x68, 0x00, 0x34 },
  312.         { 0x68, 0x45, 0x56 },
  313.         { 0x4F, 0x00, 0x27 },
  314.         { 0x4F, 0x35, 0x42 },
  315.         { 0xFF, 0x00, 0x3F },      //240
  316.         { 0xFF, 0xAA, 0xBF },
  317.         { 0xBD, 0x00, 0x2E },
  318.         { 0xBD, 0x7E, 0x8D },
  319.         { 0x81, 0x00, 0x1F },
  320.         { 0x81, 0x56, 0x60 },      //245
  321.     { 0x68, 0x00, 0x19 },
  322.         { 0x68, 0x45, 0x4E },
  323.         { 0x4F, 0x00, 0x13 },
  324.         { 0x4F, 0x35, 0x3B },
  325.         { 0x33, 0x33, 0x33 },      //250
  326.         { 0x50, 0x50, 0x50 },
  327.         { 0x69, 0x69, 0x69 },
  328.         { 0x82, 0x82, 0x82 },
  329.         { 0xBE, 0xBE, 0xBE },
  330.         { 0xFF, 0xFF, 0xFF }      //255
  331. };
  332.  
  333. const unsigned short DXFColor::numberOfColors = sizeof(DXFColor::DXFColors) / sizeof(DXFColor::rgbcolor);
  334.  
  335.  
  336.  
  337. drvDXF::drvDXF(const char * driveroptions_p,ostream & theoutStream, ostream & theerrStream/* , float theMagnification */ ):
  338.         drvbase(driveroptions_p,theoutStream,theerrStream,0,0,0)
  339. /*    ,magnification(theMagnification) */
  340. {
  341. outf << "999\nDXF generated by pstoedit\n";
  342. outf << "  0\nSECTION\n  2\nHEADER\n";
  343. outf << "  9\n$ACADVER\n  1\nAC1009\n";
  344. outf << "  9\n$EXTMIN\n 10\n0.0\n 20\n0.0\n 30\n0.0\n";
  345. outf << "  9\n$EXTMAX\n 10\n1000.0\n 20\n1000.0\n 30\n0.0\n";
  346. outf << "  9\n$FILLMODE\n 70\n     0\n";
  347. outf << "  0\nENDSEC\n";
  348.  
  349. outf << "  0\nSECTION\n  2\nTABLES\n";
  350. // Layers
  351. outf << "  0\nTABLE\n  2\nLAYER\n 70\n   255\n" ;
  352. outf << "  0\nLAYER\n  2\n0\n 70\n     0\n 62\n     7\n  6\nCONTINUOUS\n";
  353. outf << "  0\nENDTAB\n";
  354. outf << "  0\nENDSEC\n";
  355.  
  356. outf << "  0\nSECTION\n  2\nENTITIES\n";
  357. }
  358.  
  359. drvDXF::~drvDXF() 
  360. {
  361. outf << "  0\nENDSEC\n  0\nEOF\n";
  362. }
  363.  
  364. void drvDXF::print_coords()
  365. {
  366. }
  367.  
  368. void drvDXF::close_page()
  369. {
  370. /*outf << "#Seite beendet.\n";*/
  371. }
  372.  
  373. void drvDXF::open_page()
  374. {
  375. /*outf << "#Seite Nr. " << currentPageNumber << "\n";*/
  376. }
  377.  
  378. void drvDXF::show_text(const TextInfo & textinfo)
  379. {
  380.     outf << "  0\nTEXT\n  8\n0\n";
  381.     // color
  382.     outf << " 62\n     " << DXFColor::getDXFColor(textinfo.currentR, textinfo.currentG, textinfo.currentB)
  383.          << "\n";
  384.     outf << " 10\n" << textinfo.x << "\n";
  385.     outf << " 20\n" << textinfo.y << "\n";
  386.     outf << " 30\n" << 0.0 << "\n";
  387.     outf << " 40\n" << textinfo.currentFontSize << "\n";
  388.     outf << "  1\n" << textinfo.thetext << "\n";
  389.     outf << " 50\n" << textinfo.currentFontAngle << "\n";
  390. }
  391.  
  392. void drvDXF::show_path()
  393. {
  394. #ifdef aslines
  395.     for(unsigned int t=1;t<numberOfElementsInPath();t++) {
  396.         const Point & p = pathElement(t-1).getPoint(0);
  397.         const Point & q = pathElement(t).getPoint(0);
  398.         outf << "  0\nLINE\n  8\n0\n";
  399.         outf << " 10\n" << p.x_ << "\n";
  400.         outf << " 20\n" << p.y_ << "\n";
  401.         outf << " 30\n" << 0 << "\n";
  402.         outf << " 11\n" << q.x_ << "\n";
  403.         outf << " 21\n" << q.y_ << "\n";
  404.         outf << " 31\n" << 0.0 << "\n";
  405.     }
  406. #else
  407. // to do: check for "lineto" only
  408.     const float lineWidth = currentLineWidth();
  409.     outf << "  0\nPOLYLINE\n  8\n0\n"; 
  410.     // color
  411.     outf << " 62\n     " << DXFColor::getDXFColor(currentR(), currentG(), currentB())
  412.          << "\n";
  413.     // vertex flag (always 1)
  414.     outf << " 66\n     1\n";
  415.     // base point
  416.     outf << " 10\n0.0\n 20\n0.0\n 30\n0.0\n";
  417.     if (isPolygon()) {
  418.         outf << " 70\n     1\n";
  419.     }
  420.     // start and end line width
  421.     outf << " 40\n" << lineWidth << "\n 41\n" << lineWidth << "\n";
  422.     for(unsigned int t=0;t<numberOfElementsInPath();t++) {
  423.         const Point & p = pathElement(t).getPoint(0);
  424.         outf << "  0\nVERTEX\n  8\n0\n";
  425.         outf << " 10\n" << p.x_ << "\n";
  426.         outf << " 20\n" << p.y_ << "\n";
  427.         outf << " 30\n" << 0.0 << "\n";
  428.         outf << " 40\n" << lineWidth << "\n 41\n" << lineWidth << "\n";
  429.     }
  430.     outf << "  0\nSEQEND\n";
  431. #endif
  432. };
  433.  
  434. void drvDXF::show_rectangle(const float llx, const float lly, const float urx, const float ury)
  435. {
  436. // just do show_polyline for a first guess
  437.   unused(&llx);
  438.   unused(&lly);
  439.   unused(&urx);
  440.   unused(&ury);
  441.      show_path();
  442. }
  443.