1 : /******************************************************************************
2 : * $Id: ogrdxfwriterds.cpp 18235 2009-12-09 22:33:28Z warmerdam $
3 : *
4 : * Project: DXF Translator
5 : * Purpose: Implements OGRDXFWriterDS - the OGRDataSource class used for
6 : * writing a DXF file.
7 : * Author: Frank Warmerdam, warmerdam@pobox.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "ogr_dxf.h"
32 : #include "cpl_conv.h"
33 : #include "cpl_string.h"
34 :
35 : CPL_CVSID("$Id: ogrdxfwriterds.cpp 18235 2009-12-09 22:33:28Z warmerdam $");
36 :
37 : /************************************************************************/
38 : /* OGRDXFWriterDS() */
39 : /************************************************************************/
40 :
41 0 : OGRDXFWriterDS::OGRDXFWriterDS()
42 :
43 : {
44 0 : fp = NULL;
45 0 : poLayer = NULL;
46 0 : }
47 :
48 : /************************************************************************/
49 : /* ~OGRDXFWriterDS() */
50 : /************************************************************************/
51 :
52 0 : OGRDXFWriterDS::~OGRDXFWriterDS()
53 :
54 : {
55 : /* -------------------------------------------------------------------- */
56 : /* Destroy layers. */
57 : /* -------------------------------------------------------------------- */
58 0 : delete poLayer;
59 :
60 : /* -------------------------------------------------------------------- */
61 : /* Write trailer. */
62 : /* -------------------------------------------------------------------- */
63 0 : FILE *fpSrc = VSIFOpenL( osTrailerFile, "r" );
64 0 : if( fpSrc == NULL )
65 : {
66 : CPLError( CE_Failure, CPLE_OpenFailed,
67 : "Failed to open template trailer file '%s' for reading.",
68 0 : osTrailerFile.c_str() );
69 : }
70 :
71 : /* -------------------------------------------------------------------- */
72 : /* Copy into our DXF file. */
73 : /* -------------------------------------------------------------------- */
74 : else
75 : {
76 : const char *pszLine;
77 :
78 0 : while( (pszLine = CPLReadLineL(fpSrc)) != NULL )
79 : {
80 0 : VSIFWriteL( pszLine, 1, strlen(pszLine), fp );
81 0 : VSIFWriteL( "\n", 1, 1, fp );
82 : }
83 :
84 0 : VSIFCloseL( fpSrc );
85 : }
86 :
87 : /* -------------------------------------------------------------------- */
88 : /* Close file. */
89 : /* -------------------------------------------------------------------- */
90 0 : if( fp != NULL )
91 : {
92 0 : VSIFCloseL( fp );
93 0 : fp = NULL;
94 : }
95 0 : }
96 :
97 : /************************************************************************/
98 : /* TestCapability() */
99 : /************************************************************************/
100 :
101 0 : int OGRDXFWriterDS::TestCapability( const char * pszCap )
102 :
103 : {
104 0 : if( EQUAL(pszCap,ODsCCreateLayer) )
105 0 : return TRUE;
106 : else
107 0 : return FALSE;
108 : }
109 :
110 : /************************************************************************/
111 : /* GetLayer() */
112 : /************************************************************************/
113 :
114 :
115 0 : OGRLayer *OGRDXFWriterDS::GetLayer( int iLayer )
116 :
117 : {
118 0 : if( iLayer == 0 )
119 0 : return poLayer;
120 : else
121 0 : return NULL;
122 : }
123 :
124 : /************************************************************************/
125 : /* GetLayerCount() */
126 : /************************************************************************/
127 :
128 0 : int OGRDXFWriterDS::GetLayerCount()
129 :
130 : {
131 0 : if( poLayer )
132 0 : return 1;
133 : else
134 0 : return 0;
135 : }
136 :
137 : /************************************************************************/
138 : /* Open() */
139 : /************************************************************************/
140 :
141 0 : int OGRDXFWriterDS::Open( const char * pszFilename, char **papszOptions )
142 :
143 : {
144 : /* -------------------------------------------------------------------- */
145 : /* Create the output file. */
146 : /* -------------------------------------------------------------------- */
147 0 : fp = VSIFOpenL( pszFilename, "w" );
148 :
149 0 : if( fp == NULL )
150 : {
151 : CPLError( CE_Failure, CPLE_OpenFailed,
152 : "Failed to open '%s' for writing.",
153 0 : pszFilename );
154 0 : return FALSE;
155 : }
156 :
157 : /* -------------------------------------------------------------------- */
158 : /* Open the standard header, or a user provided header. */
159 : /* -------------------------------------------------------------------- */
160 0 : CPLString osHeaderFile;
161 :
162 0 : if( CSLFetchNameValue(papszOptions,"HEADER") != NULL )
163 0 : osHeaderFile = CSLFetchNameValue(papszOptions,"HEADER");
164 : else
165 : {
166 0 : const char *pszValue = CPLFindFile( "gdal", "header.dxf" );
167 0 : if( pszValue != NULL )
168 0 : osHeaderFile = pszValue;
169 : }
170 :
171 0 : FILE *fpSrc = VSIFOpenL( osHeaderFile, "r" );
172 0 : if( fpSrc == NULL )
173 : {
174 : CPLError( CE_Failure, CPLE_OpenFailed,
175 : "Failed to open template header file '%s' for reading.",
176 0 : osHeaderFile.c_str() );
177 0 : return FALSE;
178 : }
179 :
180 : /* -------------------------------------------------------------------- */
181 : /* Copy into our DXF file. */
182 : /* -------------------------------------------------------------------- */
183 : const char *pszLine;
184 :
185 0 : while( (pszLine = CPLReadLineL(fpSrc)) != NULL )
186 : {
187 0 : VSIFWriteL( pszLine, 1, strlen(pszLine), fp );
188 0 : VSIFWriteL( "\n", 1, 1, fp );
189 : }
190 :
191 0 : VSIFCloseL( fpSrc );
192 :
193 : /* -------------------------------------------------------------------- */
194 : /* Establish the name for our trailer file. */
195 : /* -------------------------------------------------------------------- */
196 0 : if( CSLFetchNameValue(papszOptions,"TRAILER") != NULL )
197 0 : osTrailerFile = CSLFetchNameValue(papszOptions,"TRAILER");
198 : else
199 : {
200 0 : const char *pszValue = CPLFindFile( "gdal", "trailer.dxf" );
201 0 : if( pszValue != NULL )
202 0 : osTrailerFile = pszValue;
203 : }
204 :
205 0 : return TRUE;
206 : }
207 :
208 : /************************************************************************/
209 : /* CreateLayer() */
210 : /************************************************************************/
211 :
212 0 : OGRLayer *OGRDXFWriterDS::CreateLayer( const char *,
213 : OGRSpatialReference *,
214 : OGRwkbGeometryType,
215 : char ** )
216 :
217 : {
218 0 : if( poLayer == NULL )
219 : {
220 0 : poLayer = new OGRDXFWriterLayer( fp );
221 0 : return poLayer;
222 : }
223 : else
224 : {
225 : CPLError( CE_Failure, CPLE_AppDefined,
226 0 : "Unable to more than one OGR layer in a DXF file." );
227 0 : return NULL;
228 : }
229 : }
|