1 : /******************************************************************************
2 : * $Id: ogrdxfwriterds.cpp 18578 2010-01-18 14:48:56Z 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 18578 2010-01-18 14:48:56Z warmerdam $");
36 :
37 : /************************************************************************/
38 : /* OGRDXFWriterDS() */
39 : /************************************************************************/
40 :
41 1 : OGRDXFWriterDS::OGRDXFWriterDS()
42 :
43 : {
44 1 : fp = NULL;
45 1 : poLayer = NULL;
46 1 : }
47 :
48 : /************************************************************************/
49 : /* ~OGRDXFWriterDS() */
50 : /************************************************************************/
51 :
52 1 : OGRDXFWriterDS::~OGRDXFWriterDS()
53 :
54 : {
55 : /* -------------------------------------------------------------------- */
56 : /* Destroy layers. */
57 : /* -------------------------------------------------------------------- */
58 1 : delete poLayer;
59 :
60 : /* -------------------------------------------------------------------- */
61 : /* Write trailer. */
62 : /* -------------------------------------------------------------------- */
63 1 : if( osTrailerFile != "" )
64 : {
65 1 : FILE *fpSrc = VSIFOpenL( osTrailerFile, "r" );
66 :
67 1 : if( fpSrc == NULL )
68 : {
69 : CPLError( CE_Failure, CPLE_OpenFailed,
70 : "Failed to open template trailer file '%s' for reading.",
71 0 : osTrailerFile.c_str() );
72 : }
73 :
74 : /* -------------------------------------------------------------------- */
75 : /* Copy into our DXF file. */
76 : /* -------------------------------------------------------------------- */
77 : else
78 : {
79 : const char *pszLine;
80 :
81 2164 : while( (pszLine = CPLReadLineL(fpSrc)) != NULL )
82 : {
83 2162 : VSIFWriteL( pszLine, 1, strlen(pszLine), fp );
84 2162 : VSIFWriteL( "\n", 1, 1, fp );
85 : }
86 :
87 1 : VSIFCloseL( fpSrc );
88 : }
89 : }
90 :
91 : /* -------------------------------------------------------------------- */
92 : /* Close file. */
93 : /* -------------------------------------------------------------------- */
94 1 : if( fp != NULL )
95 : {
96 1 : VSIFCloseL( fp );
97 1 : fp = NULL;
98 : }
99 1 : }
100 :
101 : /************************************************************************/
102 : /* TestCapability() */
103 : /************************************************************************/
104 :
105 0 : int OGRDXFWriterDS::TestCapability( const char * pszCap )
106 :
107 : {
108 0 : if( EQUAL(pszCap,ODsCCreateLayer) )
109 0 : return TRUE;
110 : else
111 0 : return FALSE;
112 : }
113 :
114 : /************************************************************************/
115 : /* GetLayer() */
116 : /************************************************************************/
117 :
118 :
119 0 : OGRLayer *OGRDXFWriterDS::GetLayer( int iLayer )
120 :
121 : {
122 0 : if( iLayer == 0 )
123 0 : return poLayer;
124 : else
125 0 : return NULL;
126 : }
127 :
128 : /************************************************************************/
129 : /* GetLayerCount() */
130 : /************************************************************************/
131 :
132 0 : int OGRDXFWriterDS::GetLayerCount()
133 :
134 : {
135 0 : if( poLayer )
136 0 : return 1;
137 : else
138 0 : return 0;
139 : }
140 :
141 : /************************************************************************/
142 : /* Open() */
143 : /************************************************************************/
144 :
145 1 : int OGRDXFWriterDS::Open( const char * pszFilename, char **papszOptions )
146 :
147 : {
148 : /* -------------------------------------------------------------------- */
149 : /* Open the standard header, or a user provided header. */
150 : /* -------------------------------------------------------------------- */
151 1 : CPLString osHeaderFile;
152 :
153 1 : if( CSLFetchNameValue(papszOptions,"HEADER") != NULL )
154 0 : osHeaderFile = CSLFetchNameValue(papszOptions,"HEADER");
155 : else
156 : {
157 1 : const char *pszValue = CPLFindFile( "gdal", "header.dxf" );
158 1 : if( pszValue == NULL )
159 : {
160 : CPLError( CE_Failure, CPLE_OpenFailed,
161 0 : "Failed to find template header file header.dxf for reading,\nis GDAL_DATA set properly?" );
162 0 : return FALSE;
163 : }
164 1 : osHeaderFile = pszValue;
165 : }
166 :
167 : /* -------------------------------------------------------------------- */
168 : /* Create the output file. */
169 : /* -------------------------------------------------------------------- */
170 1 : fp = VSIFOpenL( pszFilename, "w" );
171 :
172 1 : if( fp == NULL )
173 : {
174 : CPLError( CE_Failure, CPLE_OpenFailed,
175 : "Failed to open '%s' for writing.",
176 0 : pszFilename );
177 0 : return FALSE;
178 : }
179 :
180 : /* -------------------------------------------------------------------- */
181 : /* Open the template file. */
182 : /* -------------------------------------------------------------------- */
183 1 : FILE *fpSrc = VSIFOpenL( osHeaderFile, "r" );
184 1 : if( fpSrc == NULL )
185 : {
186 : CPLError( CE_Failure, CPLE_OpenFailed,
187 : "Failed to open template header file '%s' for reading.",
188 0 : osHeaderFile.c_str() );
189 0 : return FALSE;
190 : }
191 :
192 : /* -------------------------------------------------------------------- */
193 : /* Copy into our DXF file. */
194 : /* -------------------------------------------------------------------- */
195 : const char *pszLine;
196 :
197 1546 : while( (pszLine = CPLReadLineL(fpSrc)) != NULL )
198 : {
199 1544 : VSIFWriteL( pszLine, 1, strlen(pszLine), fp );
200 1544 : VSIFWriteL( "\n", 1, 1, fp );
201 : }
202 :
203 1 : VSIFCloseL( fpSrc );
204 :
205 : /* -------------------------------------------------------------------- */
206 : /* Establish the name for our trailer file. */
207 : /* -------------------------------------------------------------------- */
208 1 : if( CSLFetchNameValue(papszOptions,"TRAILER") != NULL )
209 0 : osTrailerFile = CSLFetchNameValue(papszOptions,"TRAILER");
210 : else
211 : {
212 1 : const char *pszValue = CPLFindFile( "gdal", "trailer.dxf" );
213 1 : if( pszValue != NULL )
214 1 : osTrailerFile = pszValue;
215 : }
216 :
217 1 : return TRUE;
218 : }
219 :
220 : /************************************************************************/
221 : /* CreateLayer() */
222 : /************************************************************************/
223 :
224 : OGRLayer *OGRDXFWriterDS::CreateLayer( const char *,
225 : OGRSpatialReference *,
226 : OGRwkbGeometryType,
227 1 : char ** )
228 :
229 : {
230 1 : if( poLayer == NULL )
231 : {
232 1 : poLayer = new OGRDXFWriterLayer( fp );
233 1 : return poLayer;
234 : }
235 : else
236 : {
237 : CPLError( CE_Failure, CPLE_AppDefined,
238 0 : "Unable to more than one OGR layer in a DXF file." );
239 0 : return NULL;
240 : }
241 : }
|