Home » Source Code » » dl_dxf.cpp

dl_dxf.cpp ( File view )

From:
  • By 2010-08-07
  • View(s):23
  • Download(s):0
  • Point(s): 1
			/****************************************************************************
** $Id: dl_dxf.cpp 8865 2008-02-04 18:54:02Z andrew $
**
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
** This file is part of the dxflib project.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid dxflib Professional Edition licenses may use 
** this file in accordance with the dxflib Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.ribbonsoft.com for further details.
**
** Contact info@ribbonsoft.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include <stdafx.h>
#include "dl_dxf.h"

#include <algorithm>
#include <string>
#include <cstdio>
#include <cassert>
#include <cmath>

#include "dl_attributes.h"
#include "dl_codes.h"
#include "dl_creationinterface.h"
#include "dl_writer_ascii.h"


/**
 * Default constructor.
 */
DL_Dxf::DL_Dxf() {

    styleHandleStd = 0;
    version = VER_2000;

    vertices = NULL;
    maxVertices = 0;
    vertexIndex = 0;

    knots = NULL;
    maxKnots = 0;
    knotIndex = 0;

    controlPoints = NULL;
    maxControlPoints = 0;
    controlPointIndex = 0;

    leaderVertices = NULL;
    maxLeaderVertices = 0;
    leaderVertexIndex = 0;

    hatchLoops = NULL;
    maxHatchLoops = 0;
    hatchLoopIndex = -1;
    hatchEdges = NULL;
    maxHatchEdges = NULL;
    hatchEdgeIndex = NULL;
    dropEdges = false;

}



/**
 * Destructor.
 */
DL_Dxf::~DL_Dxf() {

    if (vertices!=NULL) {

        delete[] vertices;
    
}
    if (knots!=NULL) {

        delete[] knots;
    
}
    if (controlPoints!=NULL) {

        delete[] controlPoints;
    
}
    if (leaderVertices!=NULL) {

        delete[] leaderVertices;
    
}
    if (hatchLoops!=NULL) {

        delete[] hatchLoops;
    
}
    if (hatchEdges!=NULL) {

        for (int i=0; i<maxHatchLoops; ++i) {

            if (hatchEdges[i]!=NULL) {

                delete[] hatchEdges[i];
            
}
        
}
        delete[] hatchEdges;
    
}
    if (maxHatchEdges!=NULL) {

        delete[] maxHatchEdges;
    
}
    if (hatchEdgeIndex!=NULL) {

        delete[] hatchEdgeIndex;
    
}

}



/**
 * @brief Reads the given file and calls the appropriate functions in
 * the given creation interface for every entity found in the file.
 *
 * @param file Input
 *		Path and name of file to read
 * @param creationInterface
 *		Pointer to the class which takes care of the entities in the file.
 *
 * @retval true If \p file could be opened.
 * @retval false If \p file could not be opened.
 */
bool DL_Dxf::in(const string& file, DL_CreationInterface* creationInterface) {

    FILE *fp;
    firstCall = true;
    currentEntity = DL_UNKNOWN;
    int errorCounter = 0;

    fp = fopen(file.c_str(), "rt");
    if (fp) {

        while (readDxfGroups(fp, creationInterface, &errorCounter)) {

}
        fclose(fp);
        if (errorCounter>0) {

            std::cerr << "DXF Filter: There have been " << errorCounter <<
            " errors. The drawing might be incomplete / incorrect.\n";
        
}
        return true;
    
}

    return false;

}



/**
 * Reads a DXF file from an existing stream.
 *
 * @param stream The string stream.
 * @param creationInterface
 *		Pointer to the class which takes care of the entities in the file.
 *
 * @retval true If \p file could be opened.
 * @retval false If \p file could not be opened.
 */
#ifndef __GCC2x__
bool DL_Dxf::in(std::stringstream& stream,
                DL_CreationInterface* creationInterface) {

    
    int errorCounter = 0;

    if (stream.good()) {

        firstCall=true;
        currentEntity = DL_UNKNOWN;
        while (readDxfGroups(stream, creationInterface, &errorCounter)) {

}
        if (errorCounter>0) {

            std::cerr << "DXF Filter: There have been " << errorCounter <<
            " errors. The drawing might be incomplete / incorrect.\n";
        
}
        return true;
    
}
    return false;

}
#endif



/**
 * @brief Reads a group couplet from a DXF file.  Calls another function
 * to process it.
 *
 * A group couplet consists of two lines that represent a single
 * piece of data.  An integer constant on the first line indicates
 * the type of data.  The value is on the next line.\n
 *
 * This function reads a couplet, determines the type of data, and
 * passes the value to the the appropriate handler function of
 * \p creationInterface.\n
 * 
 * \p fp is advanced so that the next call to \p readDXFGroups() reads
 * the next couplet in the file.
 *
 * @param fp Handle of input file
 * @param creationInterface Handle of class which processes entities
 *		in the file
 *
 * @retval true If EOF not reached.
 * @retval false If EOF reached.
 */
bool DL_Dxf::readDxfGroups(FILE *fp, DL_CreationInterface* creationInterface,
                           int* errorCounter) {


    bool ok = true;
    static int line = 1;

    // Read one group of the DXF file and chop the lines:
    if (DL_Dxf::getChoppedLine(groupCodeTmp, DL_DXF_MAXLINE, fp) &&
            DL_Dxf::getChoppedLine(groupValue, DL_DXF_MAXLINE, fp) ) {


        groupCode = (unsigned int)stringToInt(groupCodeTmp, &ok);

        if (ok) {

            //std::cerr << groupCode << "\n";
            //std::cerr << groupValue << "\n";
            line+=2;
            processDXFGroup(creationInterface, groupCode, groupValue);
        
} else {

            std::cerr << "DXF read error: Line: " << line << "\n";
            if (errorCounter!=NULL) {

                (*errorCounter)++;
            
}
            // try to fix:
            std::cerr << "DXF read error: trying to fix..\n";
            // drop a line to sync:
            DL_Dxf::getChoppedLine(groupCodeTmp, DL_DXF_MAXLINE, fp);
        
}
    
}

    return !feof(fp);

}



/**
 * Same as above but for stringstreams.
 */
#ifndef __GCC2x__
bool DL_Dxf::readDxfGroups(std::stringstream& stream,
                           DL_CreationInterface* creationInterface,
                           int* errorCounter) {


    bool ok = true;
    static int line = 1;

    // Read one group of the DXF file and chop the lines:
    if (DL_Dxf::getChoppedLine(groupCodeTmp, DL_DXF_MAXLINE, stream) &&
            DL_Dxf::getChoppedLine(groupValue, DL_DXF_MAXLINE, stream) ) {


        groupCode = (unsigned int)stringToInt(groupCodeTmp, &ok);

        if (ok) {

            std::cout << "group code: " << groupCode << "\n";
            std::cout << "group value: " << groupValue << "\n";
            line+=2;
            processDXFGroup(creationInterface, groupCode, groupValue);
        
} else {

            std::cerr << "DXF read error: Line: " << line << "\n";
            if (errorCounter!=NULL) {

                (*errorCounter)++;
            
}
            // try to fix:
            //std::cerr << "DXF read error: trying to fix..\n";
            // drop a line to sync:
            //DL_Dxf::getChoppedLine(groupCodeTmp, DL_DXF_MAXLINE, stream);
        
}
    
}
    return !stream.eof();

}
#endif



/**
 * @brief Reads line from file & strips whitespace at start and newline 
 * at end.
 *
 * @param s Output\n
 *		Pointer to character array that chopped line will be returned in.
 * @param size Size of \p s.  (Including space for NULL.)
 * @param fp Input\n
 *		Handle of input file.
 *
 * @retval true if line could be read
 * @retval false if \p fp is already at end of file
 *
 * @todo Change function to use safer FreeBSD strl* functions
 * @todo Is it a problem if line is blank (i.e., newline only)?
 *		Then, when function returns, (s==NULL).
 */
bool DL_Dxf::getChoppedLine(char *s, unsigned int size, FILE *fp) {

    if (!feof(fp)) {

        // The whole line in the file.  Includes space for NULL.
        char* wholeLine = new char[size];
        // Only the useful part of the line
        char* line;

        line = fgets(wholeLine, size, fp);

        if (line!=NULL && line[0] != '\0') {
 // Evaluates to fgets() retval
            // line == wholeLine at this point.
            // Both guaranteed to be NULL terminated.

            // Strip leading whitespace and trailing CR/LF.
            stripWhiteSpace(&line);

            strncpy(s, line, size);
            s[size] = '\0';
            // s should always be NULL terminated, because:
            assert(size > strlen(line));
        
}

        delete[] wholeLine; // Done with wholeLine

        return true;
    
} else {

        s[0] = '\0';
        return false;
    
}

}



/**
 * Same as above but for stringstreams.
 */
#ifndef __GCC2x__
bool DL_Dxf::getChoppedLine(char *s, unsigned int size,
                            std::stringstream& stream) {


    if (!stream.eof()) {

        // Only the useful part of the line
        char* line = new char[size+1];
        char* oriLine = line;
        stream.getline(line, size);
        stripWhiteSpace(&line);
        strncpy(s, line, size);
        s[size] = '\0';
        assert(size > strlen(s));
        delete[] oriLine;
        return true;
    
} else {

        s[0] = '\0';
        return false;
    
}

}
#endif



/**
 * @brief Strips leading whitespace and trailing Carriage Return (CR)
 * and Line Feed (LF) from NULL terminated string.
 *
 * @param s Input and output.
 *		NULL terminates string.
 *
 * @retval true if \p s is non-NULL
 * @retval false if \p s is NULL
 */
bool DL_Dxf::stripWhiteSpace(char** s) {

    // last non-NULL char:
    int lastChar = strlen(*s) - 1;

    // Is last character CR or LF?
    while ( (lastChar >= 0) &&
            (((*s)[lastChar] == 10) || ((*s)[lastChar] == 13) ||
             ((*s)[lastChar] == ' ' || ((*s)[lastChar] == '\t'))) ) {

        (*s)[lastChar] = '\0';
        lastChar--;
    
}

    // Skip whitespace, excluding \n, at beginning of line
    while ((*s)[0]==' ' || (*s)[0]=='\t') {

        ++(*s);
    
}
    
    return ((*s) ? true : false);

}



/**
 * Proc
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
源码文件 3dface.dxf3dface.dxf29.87 kB05-02-08 00:16
源码文件 <Debug><Debug>0.00 B07-04-09 20:39
源码文件 demo.bakdemo.bak98.73 kB18-09-07 13:43
源码文件 demo.dxfdemo.dxf101.99 kB06-04-09 16:18
code file dl_attributes.hdl_attributes.h3.80 kB04-04-09 16:02
code file dl_codes.hdl_codes.h20.92 kB20-11-07 01:11
code file dl_creationadapter.hdl_creationadapter.h4.07 kB04-04-09 15:03
code file dl_creationinterface.hdl_creationinterface.h7.86 kB04-04-09 15:03
code file dl_dxf.cppdl_dxf.cpp137.00 kB07-04-09 18:27
code file dl_dxf.hdl_dxf.h14.88 kB05-02-08 00:24
code file dl_entities.hdl_entities.h30.72 kB04-01-08 22:26
code file dl_exception.hdl_exception.h1.49 kB18-09-07 13:43
code file dl_extrusion.hdl_extrusion.h3.07 kB18-09-07 13:43
code file dl_writer.hdl_writer.h12.68 kB20-11-07 01:11
code file dl_writer_ascii.cppdl_writer_ascii.cpp3.35 kB30-03-09 18:53
code file dl_writer_ascii.hdl_writer_ascii.h2.21 kB18-09-07 13:43
源码文件 dxftest.apsdxftest.aps20.63 kB07-04-09 15:20
源码文件 dxftest.clwdxftest.clw1.15 kB07-04-09 18:33
code file dxftest.cppdxftest.cpp2.03 kB30-03-09 18:51
源码文件 dxftest.dspdxftest.dsp5.07 kB30-03-09 19:06
源码文件 dxftest.dswdxftest.dsw537.00 B30-03-09 18:51
code file dxftest.hdxftest.h1.30 kB30-03-09 18:51
源码文件 dxftest.ncbdxftest.ncb577.00 kB07-04-09 18:33
源码文件 dxftest.optdxftest.opt55.50 kB07-04-09 18:33
源码文件 dxftest.plgdxftest.plg1.01 kB07-04-09 18:29
code file dxftest.rcdxftest.rc5.30 kB06-04-09 20:26
code file dxftestDlg.cppdxftestDlg.cpp6.10 kB06-04-09 21:01
code file dxftestDlg.hdxftestDlg.h1.39 kB04-04-09 20:06
code file main.hmain.h8.09 kB07-04-09 18:25
code file memdc.hmemdc.h3.79 kB07-04-09 15:22
源码文件 myfile.dxfmyfile.dxf12.62 kB05-02-08 00:23
code file ReadMe.txtReadMe.txt3.51 kB30-03-09 18:51
源码文件 dxftest.icodxftest.ico1.05 kB30-03-09 18:51
源码文件 dxftest.rc2dxftest.rc2399.00 B30-03-09 18:51
源码文件 Thumbs.dbThumbs.db3.00 kB30-03-09 19:04
源码文件 <res><res>0.00 B07-04-09 20:39
code file resource.hresource.h731.00 B04-04-09 20:06
源码文件 RF67_Fl200_2.dxfRF67_Fl200_2.dxf38.69 kB19-01-00 16:55
源码文件 sample.dxfsample.dxf2.71 kB21-11-03 22:07
code file StdAfx.cppStdAfx.cpp209.00 B30-03-09 18:51
code file StdAfx.hStdAfx.h1.03 kB30-03-09 18:51
code file test_creationclass.cpptest_creationclass.cpp9.62 kB07-04-09 17:27
code file test_creationclass.htest_creationclass.h2.44 kB06-04-09 21:00
源码文件 <dxftest><dxftest>0.00 B07-04-09 20:39
源码文件 <DXF><DXF>0.00 B07-04-09 20:39
...
Sponsored links
×

dl_dxf.cpp (186.88 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

×

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com
×

切换到中文版?

CodeForge Chinese Version
CodeForge English Version
×

Where are you going?

×

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK
×

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D