KiCad PCB EDA Suite
Loading...
Searching...
No Matches
idf_parser.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014-2017 Cirilo Bernardo
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <iostream>
23#include <iomanip>
24#include <sstream>
25#include <cmath>
26#include <cerrno>
27#include <algorithm>
28#include <utility>
29#include <wx/string.h>
30#include <wx/filename.h>
31
32#include <io/idf/idf_parser.h>
33#include <io/idf/idf_helpers.h>
34#include "streamwrapper.h"
35
36#include <reporter.h>
37#include <string_utils.h>
39
40using namespace std;
41using namespace IDF3;
42
43
44static bool MatchCompOutline( IDF3_COMP_OUTLINE* aOutlineA, IDF3_COMP_OUTLINE* aOutlineB )
45{
46 if( aOutlineA->GetComponentClass() != aOutlineB->GetComponentClass() )
47 return false;
48
49 if( aOutlineA->OutlinesSize() != aOutlineB->OutlinesSize() )
50 return false;
51
52 // are both outlines empty?
53 if( aOutlineA->OutlinesSize() == 0 )
54 return true;
55
56 IDF_OUTLINE* opA = aOutlineA->GetOutline( 0 );
57 IDF_OUTLINE* opB = aOutlineB->GetOutline( 0 );
58
59 if( opA->size() != opB->size() )
60 return false;
61
62 if( opA->size() == 0 )
63 return true;
64
65 std::list<IDF_SEGMENT*>::iterator olAs = opA->begin();
66 std::list<IDF_SEGMENT*>::iterator olAe = opA->end();
67 std::list<IDF_SEGMENT*>::iterator olBs = opB->begin();
68
69 while( olAs != olAe )
70 {
71 if( !(*olAs)->MatchesStart( (*olBs)->startPoint ) )
72 return false;
73
74 if( !(*olAs)->MatchesEnd( (*olBs)->endPoint ) )
75 return false;
76
77 ++olAs;
78 ++olBs;
79 }
80
81 return true;
82}
83
84
86{
87 parent = nullptr;
88 outline = nullptr;
89 xoff = 0.0;
90 yoff = 0.0;
91 zoff = 0.0;
92 aoff = 0.0;
93}
94
95
97 IDF3_COMP_OUTLINE* aOutline )
98{
99 parent = aParent;
100 outline = aOutline;
101 xoff = 0.0;
102 yoff = 0.0;
103 zoff = 0.0;
104 aoff = 0.0;
105
106 if( aOutline )
107 aOutline->incrementRef();
108}
109
110
112 IDF3_COMP_OUTLINE* aOutline,
113 double aXoff, double aYoff,
114 double aZoff, double aAngleOff )
115{
116 parent = aParent;
117 outline = aOutline;
118 xoff = aXoff;
119 yoff = aYoff;
120 zoff = aZoff;
121 aoff = aAngleOff;
122}
123
124
126{
127 if( outline )
128 outline->decrementRef();
129}
130
131
132#ifndef DISABLE_IDF_OWNERSHIP
133bool IDF3_COMP_OUTLINE_DATA::checkOwnership( int aSourceLine, const char* aSourceFunc )
134{
135 if( !parent )
136 {
137 ostringstream ostr;
138 ostr << __FILE__ << ":" << aSourceLine << ":" << aSourceFunc << "():\n";
139 ostr << "* BUG: IDF3_COMP_OUTLINE_DATA::parent not set; cannot enforce ownership rules\n";
140 errormsg = ostr.str();
141
142 return false;
143 }
144
145 IDF3::IDF_PLACEMENT placement = parent->GetPlacement();
146 IDF3::CAD_TYPE parentCAD = parent->GetCadType();
147
148 if( placement == IDF_PLACEMENT::PS_PLACED || placement == IDF_PLACEMENT::PS_UNPLACED )
149 return true;
150
151 if( placement == IDF_PLACEMENT::PS_MCAD && parentCAD == CAD_TYPE::CAD_MECH )
152 return true;
153
154 if( placement == IDF_PLACEMENT::PS_ECAD && parentCAD == CAD_TYPE::CAD_ELEC )
155 return true;
156
157 do
158 {
159 ostringstream ostr;
160 ostr << "* " << __FILE__ << ":" << aSourceLine << ":" << aSourceFunc << "():\n";
161 ostr << "* ownership violation; CAD type is ";
162
163 if( parentCAD == CAD_TYPE::CAD_MECH )
164 ostr << "MCAD ";
165 else
166 ostr << "ECAD ";
167
168 ostr << "while outline owner is " << GetPlacementString( placement ) << "\n";
169 errormsg = ostr.str();
170
171 } while( 0 );
172
173 return false;
174}
175#endif
176
177
178bool IDF3_COMP_OUTLINE_DATA::SetOffsets( double aXoff, double aYoff,
179 double aZoff, double aAngleOff )
180{
181#ifndef DISABLE_IDF_OWNERSHIP
182 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
183 return false;
184#endif
185
186 xoff = aXoff;
187 yoff = aYoff;
188 zoff = aZoff;
189 aoff = aAngleOff;
190 return true;
191}
192
193
194void IDF3_COMP_OUTLINE_DATA::GetOffsets( double& aXoff, double& aYoff,
195 double& aZoff, double& aAngleOff )
196{
197 aXoff = xoff;
198 aYoff = yoff;
199 aZoff = zoff;
200 aAngleOff = aoff;
201}
202
203
205{
206 parent = aParent;
207}
208
209
211{
212#ifndef DISABLE_IDF_OWNERSHIP
213 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
214 return false;
215#endif
216
217 if( outline )
218 outline->decrementRef();
219
220 outline = aOutline;
221
222 if( outline )
223 outline->incrementRef();
224
225 return true;
226}
227
228
229bool IDF3_COMP_OUTLINE_DATA::readPlaceData( std::istream& aBoardFile, FILE_STATE& aBoardState, IDF3_BOARD* aBoard,
230 IDF_VERSION aIdfVersion, bool aNoSubstituteOutlines )
231{
232 if( !aBoard )
233 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
234 "\n* BUG: invoked with no reference to the parent IDF_BOARD" ) );
235
236 // clear out data possibly left over from previous use of the object
237 outline = nullptr;
238 parent = nullptr;
239
240 std::string iline; // the input line
241 bool isComment; // true if a line just read in is a comment line
242 std::streampos pos;
243 int idx = 0;
244 bool quoted = false;
245 std::string token;
246 std::string uid;
247 std::string refdes;
250
251 // RECORD 2: 'package name', 'part number', 'Refdes' (any, NOREFDES, BOARD)
252 while( !FetchIDFLine( aBoardFile, iline, isComment, pos ) && aBoardFile.good() );
253
254 if( ( !aBoardFile.good() && !aBoardFile.eof() ) || iline.empty() )
255 {
256 ostringstream ostr;
257
258 ostr << "invalid IDF file\n";
259 ostr << "* violation: could not read PLACEMENT section\n";
260 ostr << "* file position: " << pos;
261
262 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
263 }
264
265 if( isComment )
266 {
267 ostringstream ostr;
268
269 ostr << "invalid IDF file\n";
270 ostr << "* violation: comment within PLACEMENT section\n";
271 ostr << "* line: '" << iline << "'\n";
272 ostr << "* file position: " << pos;
273
274 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
275 }
276
277 idx = 0;
278 GetIDFString( iline, token, quoted, idx );
279
280 if( !quoted && CompareToken( ".END_PLACEMENT", token ) )
281 {
282 aBoardState = FILE_STATE::FILE_PLACEMENT;
283 return false;
284 }
285
286 std::string ngeom = token;
287
288 if( !GetIDFString( iline, token, quoted, idx ) )
289 {
290 ostringstream ostr;
291
292 ostr << "invalid IDF file\n";
293 ostr << "* violation: no PART NAME in PLACEMENT RECORD2\n";
294 ostr << "* line: '" << iline << "'\n";
295 ostr << "* file position: " << pos;
296
297 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
298 }
299
300 std::string npart = token;
301 uid = ngeom + "_" + npart;
302
303 if( !GetIDFString( iline, token, quoted, idx ) )
304 {
305 ostringstream ostr;
306
307 ostr << "invalid IDF file\n";
308 ostr << "* violation: no REFDES in PLACEMENT RECORD2\n";
309 ostr << "* line: '" << iline << "'\n";
310 ostr << "* file position: " << pos;
311
312 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
313 }
314
315 if( CompareToken( "NOREFDES", token ) )
316 {
317 // according to the IDF3.0 specification, this is a
318 // mechanical component. The specification is defective
319 // since it is impossible to associate mechanical
320 // components with their holes unless the mechanical
321 // component is given a unique RefDes. This class of defect
322 // is one reason IDF does not work well in faithfully
323 // conveying information between ECAD and MCAD.
324 refdes = aBoard->GetNewRefDes();
325 }
326 else if( CompareToken( "BOARD", token ) )
327 {
328 ostringstream ostr;
329
330 ostr << "UNSUPPORTED FEATURE\n";
331 ostr << "* RefDes is 'BOARD', indicating this is a PANEL FILE (not supported)\n";
332 ostr << "* line: '" << iline << "'\n";
333 ostr << "* file position: " << pos;
334
335 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
336 }
337 else if( CompareToken( "PANEL", token ) )
338 {
339 ostringstream ostr;
340
341 ostr << "invalid IDF file\n";
342 ostr << "* violation: RefDes in PLACEMENT RECORD2 is 'PANEL'\n";
343 ostr << "* line: '" << iline << "'\n";
344 ostr << "* file position: " << pos;
345
346 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
347 }
348 else if( token.empty() )
349 {
350 ostringstream ostr;
351
352 ostr << "invalid IDF file\n";
353 ostr << "* violation: empty RefDes string in PLACEMENT RECORD2\n";
354 ostr << "* line: '" << iline << "'\n";
355 ostr << "* file position: " << pos;
356
357 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
358 }
359 else
360 {
361 // note: perversely, spaces can be a valid RefDes
362 refdes = token;
363 }
364
365 // V2: RECORD 3: X, Y, ROT, SIDE (top/bot), PLACEMENT (fixed, placed, unplaced)
366 // V3: RECORD 3: X, Y, Z, ROT, SIDE (top/bot), PLACEMENT (placed, unplaced, mcad, ecad)
367 while( !FetchIDFLine( aBoardFile, iline, isComment, pos ) && aBoardFile.good() );
368
369 if( !aBoardFile.good() )
370 {
371 ostringstream ostr;
372
373 ostr << "invalid IDF file\n";
374 ostr << "* problems reading PLACEMENT SECTION, RECORD 3\n";
375 ostr << "* file position: " << pos;
376
377 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
378 }
379
380 if( isComment )
381 {
382 ostringstream ostr;
383
384 ostr << "invalid IDF file\n";
385 ostr << "* violation: comment within PLACEMENT section\n";
386 ostr << "* line: '" << iline << "'\n";
387 ostr << "* file position: " << pos;
388
389 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
390 }
391
392 idx = 0;
393 GetIDFString( iline, token, quoted, idx );
394
395 if( quoted )
396 {
397 ostringstream ostr;
398
399 ostr << "invalid IDF file\n";
400 ostr << "* violation: X value must not be in quotes (PLACEMENT RECORD 3)\n";
401 ostr << "* line: '" << iline << "'\n";
402 ostr << "* file position: " << pos;
403
404 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
405 }
406
407 istringstream istr;
408 istr.str( token );
409
410 istr >> xoff;
411 if( istr.fail() )
412 {
413 ostringstream ostr;
414
415 ostr << "invalid IDF file\n";
416 ostr << "* violation: X value is not numeric (PLACEMENT RECORD 3)\n";
417 ostr << "* line: '" << iline << "'\n";
418 ostr << "* file position: " << pos;
419
420 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
421 }
422
423 if( !GetIDFString( iline, token, quoted, idx ) )
424 {
425 ostringstream ostr;
426
427 ostr << "invalid IDF file\n";
428 ostr << "* violation: no Y value (PLACEMENT RECORD 3)\n";
429 ostr << "* line: '" << iline << "'\n";
430 ostr << "* file position: " << pos;
431
432 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
433 }
434
435 istr.clear();
436 istr.str( token );
437
438 istr >> yoff;
439 if( istr.fail() )
440 {
441 ostringstream ostr;
442
443 ostr << "invalid IDF file\n";
444 ostr << "* violation: Y value is not numeric (PLACEMENT RECORD 3)\n";
445 ostr << "* line: '" << iline << "'\n";
446 ostr << "* file position: " << pos;
447
448 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
449 }
450
451 if( aIdfVersion > IDF_VERSION::IDF_V2 )
452 {
453 if( !GetIDFString( iline, token, quoted, idx ) )
454 {
455 ostringstream ostr;
456
457 ostr << "invalid IDFv3 file\n";
458 ostr << "* violation: no Z value (PLACEMENT RECORD 3)\n";
459 ostr << "* line: '" << iline << "'\n";
460 ostr << "* file position: " << pos;
461
462 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
463 }
464
465 istr.clear();
466 istr.str( token );
467
468 istr >> zoff;
469 if( istr.fail() )
470 {
471 ostringstream ostr;
472
473 ostr << "invalid IDFv3 file\n";
474 ostr << "* violation: Z value is not numeric (PLACEMENT RECORD 3)\n";
475 ostr << "* line: '" << iline << "'\n";
476 ostr << "* file position: " << pos;
477
478 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
479 }
480 }
481
482 if( !GetIDFString( iline, token, quoted, idx ) )
483 {
484 ostringstream ostr;
485
486 ostr << "invalid IDF file\n";
487 ostr << "* violation: no rotation value (PLACEMENT RECORD 3)\n";
488 ostr << "* line: '" << iline << "'\n";
489 ostr << "* file position: " << pos;
490
491 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
492 }
493
494 istr.clear();
495 istr.str( token );
496
497 istr >> aoff;
498 if( istr.fail() )
499 {
500 ostringstream ostr;
501
502 ostr << "invalid IDF file\n";
503 ostr << "* violation: rotation value is not numeric (PLACEMENT RECORD 3)\n";
504 ostr << "* line: '" << iline << "'\n";
505 ostr << "* file position: " << pos;
506
507 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
508 }
509
510 if( !GetIDFString( iline, token, quoted, idx ) )
511 {
512 ostringstream ostr;
513
514 ostr << "invalid IDF file\n";
515 ostr << "* violation: no SIDE value (PLACEMENT RECORD 3)\n";
516 ostr << "* line: '" << iline << "'\n";
517 ostr << "* file position: " << pos;
518
519 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
520 }
521
522 if( CompareToken( "TOP", token ) )
523 {
525 }
526 else if( CompareToken( "BOTTOM", token ) )
527 {
529 }
530 else
531 {
532 ostringstream ostr;
533
534 ostr << "invalid IDF file\n";
535 ostr << "* violation: invalid SIDE value in PLACEMENT RECORD 3 ('";
536 ostr << token << "'); must be one of TOP/BOTTOM\n";
537 ostr << "* line: '" << iline << "'\n";
538 ostr << "* file position: " << pos;
539
540 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
541 }
542
543 if( !GetIDFString( iline, token, quoted, idx ) )
544 {
545 ostringstream ostr;
546
547 ostr << "invalid IDF file\n";
548 ostr << "* violation: no PLACEMENT value in PLACEMENT RECORD 3\n";
549 ostr << "* line: '" << iline << "'\n";
550 ostr << "* file position: " << pos;
551
552 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
553 }
554
555 if( CompareToken( "PLACED", token ) )
556 {
558 }
559 else if( CompareToken( "UNPLACED", token ) )
560 {
562 }
563 else if( aIdfVersion > IDF_VERSION::IDF_V2 && CompareToken( "MCAD", token ) )
564 {
566 }
567 else if( aIdfVersion > IDF_VERSION::IDF_V2 && CompareToken( "ECAD", token ) )
568 {
570 }
571 else if( aIdfVersion < IDF_VERSION::IDF_V3 && CompareToken( "FIXED", token ) )
572 {
573 if( aBoard->GetCadType() == CAD_TYPE::CAD_ELEC )
575 else
577 }
578 else
579 {
580 ostringstream ostr;
581
582 ostr << "invalid IDF file\n";
583 ostr << "* violation: invalid PLACEMENT value ('";
584 ostr << token << "') in PLACEMENT RECORD 3\n";
585 ostr << "* line: '" << iline << "'\n";
586 ostr << "* file position: " << pos;
587
588 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
589 }
590
591 outline = aBoard->GetComponentOutline( uid );
592
593 if( outline == nullptr && !aNoSubstituteOutlines )
594 {
595 ERROR_IDF << "MISSING OUTLINE\n";
596 cerr << "* GeomName( " << ngeom << " ), PartName( " << npart << " )\n";
597 cerr << "* Substituting default outline.\n";
598 outline = aBoard->GetInvalidOutline( ngeom, npart );
599
600 if( outline == nullptr )
601 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
602 "\n* missing outline: cannot create default" ) );
603 }
604
605 if( aBoard->GetUnit() == IDF3::IDF_UNIT::UNIT_THOU )
606 {
610 }
611
612 parent = aBoard->FindComponent( refdes );
613
614 if( parent == nullptr )
615 {
616 IDF3_COMPONENT* cp;
617
618 try
619 {
620 cp = new IDF3_COMPONENT( aBoard );
621 }
622 catch( std::bad_alloc& )
623 {
624 outline = nullptr;
625
626 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
627 "cannot create component object" ) );
628 }
629
630 cp->SetRefDes( refdes );
631 cp->SetPosition( xoff, yoff, aoff, side );
632 cp->SetPlacement( placement );
633
634 xoff = 0;
635 yoff = 0;
636 aoff = 0;
637
638 aBoard->AddComponent( cp );
639
640 parent = cp;
641 }
642 else
643 {
644 double tX, tY, tA;
646
647 if( parent->GetPosition( tX, tY, tA, tL ) )
648 {
649 if( side != tL )
650 {
651 outline = nullptr;
652 ostringstream ostr;
653
654 ostr << "invalid IDF file\n";
655 ostr << "* violation: inconsistent PLACEMENT data; ";
656 ostr << "* SIDE value has changed from " << GetLayerString( tL );
657 ostr << " to " << GetLayerString( side ) << "\n";
658 ostr << "* line: '" << iline << "'\n";
659 ostr << "* file position: " << pos;
660
661 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
662 }
663
664 xoff -= tX;
665 yoff -= tY;
666 aoff -= tA;
667 }
668 else
669 {
670 parent->SetPosition( xoff, yoff, aoff, side );
671 parent->SetPlacement( placement );
672
673 xoff = 0;
674 yoff = 0;
675 aoff = 0;
676 }
677
678 if( placement != parent->GetPlacement() )
679 {
680 outline = nullptr;
681 ostringstream ostr;
682
683 ostr << "invalid IDF file\n";
684 ostr << "* violation: inconsistent PLACEMENT data; ";
685 ostr << "* PLACEMENT value has changed from ";
686 ostr << GetPlacementString( parent->GetPlacement() );
687 ostr << " to " << GetPlacementString( placement ) << "\n";
688 ostr << "* line: '" << iline << "'\n";
689 ostr << "* file position: " << pos;
690
691 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
692 }
693
694 }
695
696 // copy internal data to a new object and push it into the component's outline list
698 *cdp = *this;
699 if( outline ) outline->incrementRef();
700 outline = nullptr;
701
702 if( !parent->AddOutlineData( cdp ) )
703 {
704 delete cdp;
705
706 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
707 "could not add outline data object" ) );
708 }
709
710 return true;
711}
712
713
714void IDF3_COMP_OUTLINE_DATA::writePlaceData( std::ostream& aBoardFile,
715 double aXpos, double aYpos, double aAngle,
716 const std::string& aRefDes,
717 IDF3::IDF_PLACEMENT aPlacement,
718 IDF3::IDF_LAYER aSide )
719{
720 if( outline == nullptr )
721 return;
722
723 if( outline->GetUID().empty() )
724 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
725 "empty GEOM and PART names" ) );
726
727 if( aPlacement == IDF_PLACEMENT::PS_INVALID )
728 {
729 ERROR_IDF << "placement invalid (" << aRefDes << ":";
730 aPlacement = IDF_PLACEMENT::PS_PLACED;
731 }
732
733 if( aSide != IDF_LAYER::LYR_TOP && aSide != IDF_LAYER::LYR_BOTTOM )
734 {
735 ostringstream ostr;
736 ostr << "\n* invalid side (" << GetLayerString( aSide ) << "); ";
737 ostr << "must be TOP or BOTTOM\n";
738
739 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
740 }
741
742 // calculate the final position based on layer
743 double xpos, ypos, ang;
744
745 switch( aSide )
746 {
748 xpos = aXpos + xoff;
749 ypos = aYpos + yoff;
750 ang = aAngle + aoff;
751 break;
752
753 default:
754 xpos = aXpos - xoff;
755 ypos = aYpos + yoff;
756 ang = aAngle - aoff;
757 break;
758 }
759
760 std::string arefdes = aRefDes;
761
762 if( arefdes.empty() || !arefdes.compare( "~" )
763 || ( arefdes.size() >= 8 && CompareToken( "NOREFDES", arefdes.substr(0, 8) ) ) )
764 arefdes = "NOREFDES";
765
766 aBoardFile << "\"" << outline->GetGeomName() << "\" \"" << outline->GetPartName() << "\" "
767 << arefdes << "\n";
768
770
771 if( parent )
772 unit = parent->GetUnit();
773
774 if( unit == IDF_UNIT::UNIT_MM )
775 {
776 aBoardFile << setiosflags(ios::fixed) << setprecision(5) << xpos << " "
777 << ypos << " " << setprecision(3) << zoff << " "
778 << ang << " ";
779 }
780 else
781 {
782 aBoardFile << setiosflags(ios::fixed) << setprecision(1) << (xpos / IDF_THOU_TO_MM) << " "
783 << (ypos / IDF_THOU_TO_MM) << " " << (zoff / IDF_THOU_TO_MM) << " "
784 << setprecision(3) << ang << " ";
785 }
786
787 WriteLayersText( aBoardFile, aSide );
788
789 switch( aPlacement )
790 {
792 aBoardFile << " PLACED\n";
793 break;
794
796 aBoardFile << " UNPLACED\n";
797 break;
798
800 aBoardFile << " MCAD\n";
801 break;
802
803 default:
804 aBoardFile << " ECAD\n";
805 break;
806 }
807}
808
809
811{
812 xpos = 0.0;
813 ypos = 0.0;
814 angle = 0.0;
815
816 hasPosition = false;
819
820 parent = aParent;
821}
822
823
825{
826 std::list< IDF3_COMP_OUTLINE_DATA* >::iterator itcS = components.begin();
827 std::list< IDF3_COMP_OUTLINE_DATA* >::iterator itcE = components.end();
828
829 while( itcS != itcE )
830 {
831 delete *itcS;
832 ++itcS;
833 }
834
835 components.clear();
836
837 std::list< IDF_DRILL_DATA* >::iterator itdS = drills.begin();
838 std::list< IDF_DRILL_DATA* >::iterator itdE = drills.end();
839
840 while( itdS != itdE )
841 {
842 delete *itdS;
843 ++itdS;
844 }
845
846 drills.clear();
847}
848
849
850#ifndef DISABLE_IDF_OWNERSHIP
851bool IDF3_COMPONENT::checkOwnership( int aSourceLine, const char* aSourceFunc )
852{
853 if( !parent )
854 {
855 ostringstream ostr;
856 ostr << __FILE__ << ":" << aSourceLine << ":" << aSourceFunc << "():\n";
857 ostr << "\n* BUG: parent not set";
858 errormsg = ostr.str();
859
860 return false;
861 }
862
863 IDF3::CAD_TYPE pcad = parent->GetCadType();
864
865 switch( placement )
866 {
870 break;
871
873
874 if( pcad != CAD_TYPE::CAD_MECH )
875 {
876 ostringstream ostr;
877 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
878 ostr << "\n* ownership violation; internal CAD type (MCAD) conflicts with "
879 "PLACEMENT (";
880 ostr << GetPlacementString( placement ) << ")";
881 errormsg = ostr.str();
882
883 return false;
884 }
885
886 break;
887
889
890 if( pcad != CAD_TYPE::CAD_ELEC )
891 {
892 ostringstream ostr;
893 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
894 ostr << "\n* ownership violation; internal CAD type (MCAD) conflicts with "
895 "PLACEMENT (";
896 ostr << GetPlacementString( placement ) << ")";
897 errormsg = ostr.str();
898
899 return false;
900 }
901 break;
902
903 default:
904 do
905 {
906 ostringstream ostr;
907 ostr << "\n* BUG: unhandled internal placement value (" << static_cast<int>( placement ) << ")";
908 errormsg = ostr.str();
909
910 return false;
911 } while( 0 );
912
913 break;
914 }
915
916 return true;
917}
918#endif
919
920
922{
923 parent = aParent;
924 return;
925}
926
928{
929 if( parent )
930 return parent->GetCadType();
931
933}
934
936{
937 if( parent )
938 return parent->GetUnit();
939
941}
942
943bool IDF3_COMPONENT::SetRefDes( const std::string& aRefDes )
944{
945#ifndef DISABLE_IDF_OWNERSHIP
946 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
947 return false;
948#endif
949
950 if( aRefDes.empty() )
951 {
952 ostringstream ostr;
953 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): invalid RefDes (empty)";
954 errormsg = ostr.str();
955
956 return false;
957 }
958
959 if( CompareToken( "PANEL", aRefDes ) )
960 {
961 ostringstream ostr;
962 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
963 ostr << "* BUG: PANEL is a reserved designator and may not be used by components";
964 errormsg = ostr.str();
965
966 return false;
967 }
968
969 refdes = aRefDes;
970 return true;
971}
972
973
974const std::string& IDF3_COMPONENT::GetRefDes( void )
975{
976 return refdes;
977}
978
979
980IDF_DRILL_DATA* IDF3_COMPONENT::AddDrill( double aDia, double aXpos, double aYpos,
981 IDF3::KEY_PLATING aPlating,
982 const std::string& aHoleType,
983 IDF3::KEY_OWNER aOwner )
984{
985 IDF_DRILL_DATA* dp = nullptr;
986
987 try
988 {
989 dp = new IDF_DRILL_DATA( aDia, aXpos, aYpos, aPlating, refdes, aHoleType, aOwner );
990 }
991 catch( std::bad_alloc& )
992 {
993 return nullptr;
994 }
995
996 drills.push_back( dp );
997
998 return dp;
999}
1000
1001
1003{
1004 if( !aDrilledHole )
1005 return nullptr;
1006
1007 if( CompareToken( "PANEL", refdes ) )
1008 {
1009 ERROR_IDF;
1010 cerr << "\n* BUG: PANEL drills not supported at component level\n";
1011 return nullptr;
1012 }
1013
1014 if( refdes.compare( aDrilledHole->GetDrillRefDes() ) )
1015 {
1016 ERROR_IDF;
1017 cerr << "\n* BUG: pushing an incorrect REFDES ('" << aDrilledHole->GetDrillRefDes();
1018 cerr << "') to component ('" << refdes << "')\n";
1019 return nullptr;
1020 }
1021
1022 drills.push_back( aDrilledHole );
1023
1024 return aDrilledHole;
1025}
1026
1027
1028bool IDF3_COMPONENT::DelDrill( double aDia, double aXpos, double aYpos )
1029{
1030#ifndef DISABLE_IDF_OWNERSHIP
1031 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
1032 return false;
1033#endif
1034
1035 errormsg.clear();
1036
1037 if( drills.empty() )
1038 return false;
1039
1040 bool val = false;
1041
1042 list< IDF_DRILL_DATA* >::iterator itS = drills.begin();
1043 list< IDF_DRILL_DATA* >::iterator itE = drills.end();
1044
1045 while( !drills.empty() && itS != itE )
1046 {
1047 if( (*itS)->Matches( aDia, aXpos, aYpos ) )
1048 {
1049 val = true;
1050 delete *itS;
1051 itS = drills.erase( itS );
1052 continue;
1053 }
1054
1055 ++itS;
1056 }
1057
1058 return val;
1059}
1060
1061
1063{
1064#ifndef DISABLE_IDF_OWNERSHIP
1065 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
1066 return false;
1067#endif
1068
1069 errormsg.clear();
1070
1071 if( drills.empty() )
1072 return false;
1073
1074 list< IDF_DRILL_DATA* >::iterator itS = drills.begin();
1075 list< IDF_DRILL_DATA* >::iterator itE = drills.end();
1076
1077 while( !drills.empty() && itS != itE )
1078 {
1079 if( *itS == aDrill )
1080 {
1081 delete *itS;
1082 drills.erase( itS );
1083 return true;
1084 }
1085
1086 ++itS;
1087 }
1088
1089 return false;
1090}
1091
1092
1093const std::list<IDF_DRILL_DATA*>* IDF3_COMPONENT::GetDrills( void )
1094{
1095 return &drills;
1096}
1097
1099{
1100 if( aComponentOutline == nullptr )
1101 {
1102 ostringstream ostr;
1103 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ <<
1104 "(): invalid aComponentOutline (nullptr)";
1105 errormsg = ostr.str();
1106
1107 return false;
1108 }
1109
1110
1111 components.push_back( aComponentOutline );
1112
1113 return true;
1114}
1115
1116
1118{
1119#ifndef DISABLE_IDF_OWNERSHIP
1120 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
1121 return false;
1122#endif
1123
1124 if( components.empty() )
1125 {
1126 ostringstream ostr;
1127 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): component list is empty";
1128 errormsg = ostr.str();
1129
1130 return false;
1131 }
1132
1133 if( aComponentOutline == nullptr )
1134 {
1135 ostringstream ostr;
1136 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ <<
1137 "(): invalid aComponentOutline (nullptr)";
1138 errormsg = ostr.str();
1139
1140 return false;
1141 }
1142
1143 errormsg.clear();
1144
1145 std::list< IDF3_COMP_OUTLINE_DATA* >::iterator itS = components.begin();
1146 std::list< IDF3_COMP_OUTLINE_DATA* >::iterator itE = components.end();
1147
1148 while( itS != itE )
1149 {
1150 if( *itS == aComponentOutline )
1151 {
1152 delete *itS;
1153 components.erase( itS );
1154 return true;
1155 }
1156
1157 ++itS;
1158 }
1159
1160 return false;
1161}
1162
1163
1165{
1166#ifndef DISABLE_IDF_OWNERSHIP
1167 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
1168 return false;
1169#endif
1170
1171 if( aIndex >= components.size() )
1172 {
1173 ostringstream ostr;
1174 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
1175 ostr << "* aIndex (" << aIndex << ") out of range; list size is " << components.size();
1176 errormsg = ostr.str();
1177
1178 return false;
1179 }
1180
1181 std::list< IDF3_COMP_OUTLINE_DATA* >::iterator itS = components.begin();
1182 std::list< IDF3_COMP_OUTLINE_DATA* >::iterator itE = components.end();
1183 size_t idx = 0;
1184
1185 while( itS != itE )
1186 {
1187 if( idx == aIndex )
1188 {
1189 delete *itS;
1190 components.erase( itS );
1191 return true;
1192 }
1193
1194 ++idx;
1195 ++itS;
1196 }
1197
1198 return false;
1199}
1200
1201
1203{
1204 return components.size();
1205}
1206
1207
1208const std::list< IDF3_COMP_OUTLINE_DATA* >* IDF3_COMPONENT::GetOutlinesData( void )
1209{
1210 return &components;
1211}
1212
1213
1214bool IDF3_COMPONENT::GetPosition( double& aXpos, double& aYpos, double& aAngle,
1215 IDF3::IDF_LAYER& aLayer )
1216{
1217 errormsg.clear();
1218
1219 if( !hasPosition )
1220 {
1221 aXpos = 0.0;
1222 aYpos = 0.0;
1223 aAngle = 0.0;
1225 return false;
1226 }
1227
1228 aXpos = xpos;
1229 aYpos = ypos;
1230 aAngle = angle;
1231 aLayer = layer;
1232 return true;
1233}
1234
1235
1236bool IDF3_COMPONENT::SetPosition( double aXpos, double aYpos, double aAngle,
1237 IDF3::IDF_LAYER aLayer )
1238{
1239#ifndef DISABLE_IDF_OWNERSHIP
1240 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
1241 return false;
1242#endif
1243
1244 errormsg.clear();
1245
1246 switch( aLayer )
1247 {
1248 case IDF_LAYER::LYR_TOP:
1250 break;
1251
1252 default:
1253 do
1254 {
1255 ostringstream ostr;
1256 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
1257 ostr << "\n* invalid side (must be TOP or BOTTOM only): " <<
1258 GetLayerString( aLayer );
1259 errormsg = ostr.str();
1260
1261 return false;
1262 } while( 0 );
1263
1264 break;
1265 }
1266
1267 if( hasPosition )
1268 return false;
1269
1270 hasPosition = true;
1271 xpos = aXpos;
1272 ypos = aYpos;
1273 angle = aAngle;
1274 layer = aLayer;
1275 return true;
1276}
1277
1278
1283
1284
1286{
1287 if( aPlacementValue < IDF_PLACEMENT::PS_UNPLACED || aPlacementValue >= IDF_PLACEMENT::PS_INVALID )
1288 {
1289 ostringstream ostr;
1290 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
1291 ostr << "\n* invalid PLACEMENT value (" << static_cast<int>( aPlacementValue ) << ")";
1292 errormsg = ostr.str();
1293
1294 return false;
1295 }
1296
1297#ifndef DISABLE_IDF_OWNERSHIP
1298 if( !checkOwnership( __LINE__, __FUNCTION__ ) )
1299 return false;
1300#endif
1301
1302 placement = aPlacementValue;
1303
1304 return true;
1305}
1306
1307
1308bool IDF3_COMPONENT::writeDrillData( std::ostream& aBoardFile )
1309{
1310 if( drills.empty() )
1311 return true;
1312
1313 std::list< IDF_DRILL_DATA* >::iterator itS = drills.begin();
1314 std::list< IDF_DRILL_DATA* >::iterator itE = drills.end();
1315
1316 while( itS != itE )
1317 {
1318 (*itS)->write( aBoardFile, GetUnit() );
1319 ++itS;
1320 }
1321
1322 return true;
1323}
1324
1325
1326bool IDF3_COMPONENT::writePlaceData( std::ostream& aBoardFile )
1327{
1328 if( components.empty() )
1329 return true;
1330
1331 std::list< IDF3_COMP_OUTLINE_DATA* >::iterator itS = components.begin();
1332 std::list< IDF3_COMP_OUTLINE_DATA* >::iterator itE = components.end();
1333
1334 while( itS != itE )
1335 {
1336 (*itS)->writePlaceData( aBoardFile, xpos, ypos, angle, refdes, placement, layer );
1337 ++itS;
1338 }
1339
1340 return true;
1341}
1342
1343
1345{
1347 m_cadType = aCadType;
1348 m_userPrec = 5;
1349 m_userScale = 1.0;
1350 m_userXoff = 0.0;
1351 m_userYoff = 0.0;
1352 m_brdFileVersion = 0;
1353 m_libFileVersion = 0;
1354 m_refDesCounter = 0;
1356
1357 // unlike other outlines which are created as necessary,
1358 // the board outline always exists and its parent must
1359 // be set here
1360 m_boardOutline.setParent( this );
1361 m_boardOutline.setThickness( 1.6 );
1362
1363 return;
1364}
1365
1366
1368{
1369 Clear();
1370
1371 return;
1372}
1373
1374
1375const std::string& IDF3_BOARD::GetNewRefDes( void )
1376{
1377 ostringstream ostr;
1378 ostr << "NOREFDESn" << m_refDesCounter++;
1379
1380 m_refDesString = ostr.str();
1381
1382 return m_refDesString;
1383}
1384
1385
1386#ifndef DISABLE_IDF_OWNERSHIP
1387bool IDF3_BOARD::checkComponentOwnership( int aSourceLine, const char* aSourceFunc,
1388 IDF3_COMPONENT* aComponent )
1389{
1390 if( !aComponent )
1391 {
1392 ostringstream ostr;
1393 ostr << __FILE__ << ":" << aSourceLine << ":" << aSourceFunc;
1394 ostr << "(): Invalid component pointer (nullptr)";
1395 m_errormsg = ostr.str();
1396
1397 return false;
1398 }
1399
1400 IDF3::IDF_PLACEMENT place = aComponent->GetPlacement();
1401
1402 if( place == IDF_PLACEMENT::PS_PLACED || place == IDF_PLACEMENT::PS_UNPLACED )
1403 return true;
1404
1406 return true;
1407
1409 return true;
1410
1411 do
1412 {
1413 ostringstream ostr;
1414 ostr << "* " << __FILE__ << ":" << aSourceLine << ":" << aSourceFunc << "():\n";
1415 ostr << "* ownership violation; CAD type is ";
1416
1418 ostr << "MCAD ";
1419 else
1420 ostr << "ECAD ";
1421
1422 ostr << "while outline owner is " << GetPlacementString( place ) << "\n";
1423 m_errormsg = ostr.str();
1424
1425 } while( 0 );
1426
1427 return false;
1428}
1429#endif
1430
1431
1433{
1434 switch( aUnit )
1435 {
1436 case IDF_UNIT::UNIT_MM:
1438 m_unit = aUnit;
1439 break;
1440
1441 case IDF_UNIT::UNIT_TNM:
1442 ERROR_IDF << "\n* TNM unit is not supported; defaulting to mm\n";
1444 break;
1445
1446 default:
1447 do
1448 {
1449 ostringstream ostr;
1450 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
1451 ostr << "* invalid board unit (" << static_cast<int>( aUnit ) << ")";
1452 m_errormsg = ostr.str();
1453
1454 return false;
1455 } while( 0 );
1456
1457 break;
1458 }
1459
1460 // iterate through all owned OUTLINE objects (except IDF3_COMP_OUTLINE)
1461 // and set to the same unit
1462
1463 m_boardOutline.SetUnit( aUnit );
1464
1465 do
1466 {
1467 std::map<std::string, OTHER_OUTLINE*>::iterator its = m_otherOutlines.begin();
1468 std::map<std::string, OTHER_OUTLINE*>::iterator ite = m_otherOutlines.end();
1469
1470 while( its != ite )
1471 {
1472 its->second->SetUnit( aUnit );
1473 ++its;
1474 }
1475
1476 } while( 0 );
1477
1478 do
1479 {
1480 std::list<ROUTE_OUTLINE*>::iterator its = m_routeOutlines.begin();
1481 std::list<ROUTE_OUTLINE*>::iterator ite = m_routeOutlines.end();
1482
1483 while( its != ite )
1484 {
1485 (*its)->SetUnit( aUnit );
1486 ++its;
1487 }
1488
1489 } while( 0 );
1490
1491 do
1492 {
1493 std::list<PLACE_OUTLINE*>::iterator its = m_placeOutlines.begin();
1494 std::list<PLACE_OUTLINE*>::iterator ite = m_placeOutlines.end();
1495
1496 while( its != ite )
1497 {
1498 (*its)->SetUnit( aUnit );
1499 ++its;
1500 }
1501
1502 } while( 0 );
1503
1504 do
1505 {
1506 std::list<ROUTE_KO_OUTLINE*>::iterator its = m_routeKeepoutOutlines.begin();
1507 std::list<ROUTE_KO_OUTLINE*>::iterator ite = m_routeKeepoutOutlines.end();
1508
1509 while( its != ite )
1510 {
1511 (*its)->SetUnit( aUnit );
1512 ++its;
1513 }
1514
1515 } while( 0 );
1516
1517 do
1518 {
1519 std::list<VIA_KO_OUTLINE*>::iterator its = m_viaKeepoutOutlines.begin();
1520 std::list<VIA_KO_OUTLINE*>::iterator ite = m_viaKeepoutOutlines.end();
1521
1522 while( its != ite )
1523 {
1524 (*its)->SetUnit( aUnit );
1525 ++its;
1526 }
1527
1528 } while( 0 );
1529
1530 do
1531 {
1532 std::list<PLACE_KO_OUTLINE*>::iterator its = m_placeKeepoutOutlines.begin();
1533 std::list<PLACE_KO_OUTLINE*>::iterator ite = m_placeKeepoutOutlines.end();
1534
1535 while( its != ite )
1536 {
1537 (*its)->SetUnit( aUnit );
1538 ++its;
1539 }
1540
1541 } while( 0 );
1542
1543 do
1544 {
1545 std::multimap<std::string, GROUP_OUTLINE*>::iterator its = m_groupOutlines.begin();
1546 std::multimap<std::string, GROUP_OUTLINE*>::iterator ite = m_groupOutlines.end();
1547
1548 while( its != ite )
1549 {
1550 its->second->SetUnit( aUnit );
1551 ++its;
1552 }
1553
1554 } while( 0 );
1555
1556 //iterate through all owned IDF3_COMP_OUTLINE objects and
1557 // set to the same unit IF convert = true
1558 if( convert )
1559 {
1560 std::map<std::string, IDF3_COMP_OUTLINE*>::iterator its = m_componentOutlines.begin();
1561 std::map<std::string, IDF3_COMP_OUTLINE*>::iterator ite = m_componentOutlines.end();
1562
1563 while( its != ite )
1564 {
1565 its->second->SetUnit( aUnit );
1566 ++its;
1567 }
1568
1569 }
1570
1571 return true;
1572}
1573
1574
1575bool IDF3_BOARD::SetBoardThickness( double aBoardThickness )
1576{
1577 if( aBoardThickness <= 0.0 )
1578 {
1579 ostringstream ostr;
1580 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): ";
1581 ostr << "board thickness (" << aBoardThickness << ") must be > 0";
1582 m_errormsg = ostr.str();
1583
1584 return false;
1585 }
1586
1587 if( !m_boardOutline.SetThickness( aBoardThickness ) )
1588 {
1589 m_errormsg = m_boardOutline.GetError();
1590 return false;
1591 }
1592
1593 return true;
1594}
1595
1596
1598{
1599 return m_boardOutline.GetThickness();
1600}
1601
1602
1603void IDF3_BOARD::readBrdDrills( std::istream& aBoardFile, FILE_STATE& aBoardState )
1604{
1605 IDF_DRILL_DATA drill;
1606
1607 while( drill.read( aBoardFile, m_unit, aBoardState, m_idfVer ) )
1608 {
1610 *dp = drill;
1611
1612 if( AddDrill( dp ) == nullptr )
1613 {
1614 delete dp;
1615
1616 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1617 "\n* BUG: could not add drill data; cannot continue reading the file" ) );
1618 }
1619 }
1620}
1621
1622
1623void IDF3_BOARD::readBrdNotes( std::istream& aBoardFile, FILE_STATE& aBoardState )
1624{
1625 IDF_NOTE note;
1626
1627 while( note.readNote( aBoardFile, aBoardState, m_unit ) )
1628 {
1629 IDF_NOTE *np = new IDF_NOTE;
1630 *np = note;
1631 m_notes.push_back( np );
1632 }
1633}
1634
1635
1636void IDF3_BOARD::readBrdPlacement( std::istream& aBoardFile, FILE_STATE& aBoardState,
1637 bool aNoSubstituteOutlines )
1638{
1640
1641 while( oldata.readPlaceData( aBoardFile, aBoardState, this, m_idfVer, aNoSubstituteOutlines ) );
1642}
1643
1644
1645void IDF3_BOARD::readBrdHeader( std::istream& aBoardFile, FILE_STATE& aBoardState )
1646{
1647 std::string iline; // the input line
1648 bool isComment; // true if a line just read in is a comment line
1649 std::streampos pos;
1650 int idx = 0;
1651 bool quoted = false;
1652 std::string token;
1653
1654 // RECORD 1: ".HEADER" must be the very first line
1655 while( !FetchIDFLine( aBoardFile, iline, isComment, pos ) && aBoardFile.good() );
1656
1657 if( !aBoardFile.good() )
1658 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "problems reading board header" ) );
1659
1660 if( isComment )
1661 {
1662 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1663 "invalid IDF file\n"
1664 "* Violation of specification: first line must be .HEADER\n" ) );
1665 }
1666
1667 if( !CompareToken( ".HEADER", iline ) )
1668 {
1669 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1670 "invalid IDF file\n"
1671 "* Violation of specification:\n"
1672 "* first line must be .HEADER and have no quotes or trailing text" ) );
1673 }
1674
1675 // RECORD 2:
1676 // File Type [str]: BOARD_FILE (PANEL_FILE not supported)
1677 // IDF Version Number [float]: must be 3.0
1678 // Source System [str]: ignored
1679 // Date [str]: ignored
1680 // Board File Version [int]: ignored
1681 while( !FetchIDFLine( aBoardFile, iline, isComment, pos ) && aBoardFile.good() );
1682
1683 if( !aBoardFile.good() )
1684 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "problems reading board header, RECORD 2" ) );
1685
1686 if( isComment )
1687 {
1688 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1689 "invalid IDF file\n"
1690 "* Violation of specification: comment within .HEADER section" ) );
1691 }
1692
1693 idx = 0;
1694 GetIDFString( iline, token, quoted, idx );
1695
1696 if( quoted )
1697 {
1698 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1699 "invalid IDF file\n"
1700 "* Violation of specification:\n"
1701 "* File Type in HEADER section must not be in quotes" ) );
1702 }
1703
1704 if( !CompareToken( "BOARD_FILE", token ) )
1705 {
1706 ERROR_IDF;
1707
1708 if( CompareToken( "PANEL_FILE", token ) )
1709 {
1710 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1711 "not a board file\n"
1712 "* PANEL_FILE is not supported (expecting BOARD_FILE)" ) );
1713 }
1714 else
1715 {
1716 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1717 "invalid IDF file\n"
1718 "* Expecting string: BOARD_FILE" ) );
1719 }
1720 }
1721
1722 if( !GetIDFString( iline, token, quoted, idx ) )
1723 {
1724 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1725 "invalid IDF file\n"
1726 "* Violation of specification: HEADER section, RECORD 2: no FIELD 2" ) );
1727 }
1728
1729 if( quoted )
1730 {
1731 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1732 "invalid IDF file\n"
1733 "* Violation of specification: IDF Version must not be in quotes" ) );
1734 }
1735
1736 if( !token.compare( "3.0" ) || !token.compare( "3." ) || !token.compare( "3" ) )
1737 {
1739 }
1740 else if( !token.compare( "2.0" ) || !token.compare( "2." ) || !token.compare( "2" ) )
1741 {
1743 }
1744 else
1745 {
1746 ostringstream ostr;
1747
1748 ostr << "unsupported IDF version\n";
1749 ostr << "* Expecting version to be a variant of '3.0', '2.0' (value: '" << token << "')\n";
1750
1751 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
1752 }
1753
1754 if( !GetIDFString( iline, token, quoted, idx ) )
1755 {
1756 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1757 "invalid IDF file\n"
1758 "* Violation of specification:\n"
1759 "* HEADER section, RECORD 2, FIELD 3: no Source System string" ) );
1760 }
1761
1762 m_brdSource = token;
1763
1764 if( !GetIDFString( iline, token, quoted, idx ) )
1765 {
1766 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1767 "invalid IDF file\n"
1768 "* Violation of specification:\n"
1769 "* HEADER section, RECORD 2, FIELD 4: no Date string" ) );
1770 }
1771
1772 m_brdDate = token;
1773
1774 if( !GetIDFString( iline, token, quoted, idx ) )
1775 {
1776 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1777 "invalid IDF file\n"
1778 "* Violation of specification:\n"
1779 "* HEADER section, RECORD 2, FIELD 5: no Board File Version number" ) );
1780 }
1781
1782 std::istringstream istr;
1783 istr.str( token );
1784
1785 istr >> m_brdFileVersion;
1786
1787 if( istr.fail() )
1788 {
1789 ERROR_IDF << "invalid Board File Version in header\n";
1790 cerr << "* Setting default version of 1\n";
1791 m_brdFileVersion = 1;
1792 }
1793
1794 if( quoted )
1795 {
1796 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1797 "invalid IDF file\n"
1798 "* Violation of specification:\n"
1799 "* HEADER section, RECORD 2, FIELD 5: Board File Version must not be in quotes" ) );
1800 }
1801
1802 // RECORD 3:
1803 // Board Name [str]: stored
1804 // Units [str]: MM or THOU
1805 while( !FetchIDFLine( aBoardFile, iline, isComment, pos ) && aBoardFile.good() );
1806
1807 if( !aBoardFile.good() )
1808 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "\n* problems reading board header, RECORD 2" ) );
1809
1810 if( isComment )
1811 {
1812 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1813 "invalid IDF file\n"
1814 "* Violation of specification: comment within .HEADER section" ) );
1815 }
1816
1817 idx = 0;
1818 GetIDFString( iline, token, quoted, idx );
1819
1820 m_boardName = token;
1821
1822 if( !GetIDFString( iline, token, quoted, idx ) )
1823 {
1824 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1825 "invalid IDF file\n"
1826 "* Violation of specification:\n"
1827 "* HEADER section, RECORD 3, FIELD 1: no Board Name" ) );
1828 }
1829
1830 if( quoted )
1831 {
1832 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1833 "invalid IDF file\n"
1834 "* Violation of specification:\n"
1835 "* HEADER section, RECORD 3, FIELD 2: UNIT may not be in quotes" ) );
1836 }
1837
1838 if( CompareToken( "MM", token ) )
1839 {
1841 }
1842 else if( CompareToken( "THOU", token ) )
1843 {
1845 }
1846 else if( ( m_idfVer == IDF_VERSION::IDF_V2 ) && CompareToken( "TNM", token ) )
1847 {
1849 }
1850 else
1851 {
1852 ostringstream ostr;
1853
1854 ostr << "invalid IDF file\n";
1855 ostr << "* HEADER section, RECORD 3, FIELD 2: expecting MM or THOU (got '" << token <<
1856 "')\n";
1857
1858 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
1859 }
1860
1861 m_boardOutline.SetUnit( m_unit );
1862
1863 // RECORD 4:
1864 // .END_HEADER
1865 while( !FetchIDFLine( aBoardFile, iline, isComment, pos ) && aBoardFile.good() );
1866
1867 if( ( !aBoardFile.good() && !aBoardFile.eof() ) || iline.empty() )
1868 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "problems reading board header, RECORD 4" ) );
1869
1870 if( isComment )
1871 {
1872 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1873 "invalid IDF file\n"
1874 "* Violation of specification: comment within .HEADER section\n" ) );
1875 }
1876
1877 if( !CompareToken( ".END_HEADER", iline ) )
1878 {
1879 ostringstream ostr;
1880
1881 ostr << "invalid IDF file\n";
1882 ostr << "* Violation of specification: expected .END_HEADER\n";
1883 ostr << "* line: '" << iline << "'";
1884
1885 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
1886 }
1887
1888 aBoardState = FILE_STATE::FILE_HEADER;
1889}
1890
1891
1892void IDF3_BOARD::readBrdSection( std::istream& aBoardFile, FILE_STATE& aBoardState,
1893 bool aNoSubstituteOutlines )
1894{
1895 std::list< std::string > comments; // comments associated with a section
1896
1897 // Reads in .SECTION_ID or #COMMENTS
1898 // Expected SECTION IDs:
1899 // .BOARD_OUTLINE
1900 // .PANEL_OUTLINE (NOT SUPPORTED)
1901 // .OTHER_OUTLINE
1902 // .ROUTE_OUTLINE
1903 // .PLACE_OUTLINE
1904 // .ROUTE_KEEPOUT
1905 // .VIA_KEEPOUT
1906 // .PLACE_KEEPOUT
1907 // .PLACE_REGION
1908 // .DRILLED_HOLES
1909 // .NOTES
1910 // .PLACEMENT
1911 std::string iline; // the input line
1912 bool isComment; // true if a line just read in is a comment line
1913 std::streampos pos;
1914 int idx = 0;
1915 bool quoted = false;
1916 std::string token;
1917
1918 while( aBoardFile.good() )
1919 {
1920 while( !FetchIDFLine( aBoardFile, iline, isComment, pos ) && aBoardFile.good() );
1921
1922 if( !aBoardFile.good() )
1923 {
1924 if( aBoardFile.eof() && aBoardState >= FILE_STATE::FILE_HEADER &&
1925 aBoardState < FILE_STATE::FILE_INVALID )
1926 {
1927 if( !comments.empty() )
1928 ERROR_IDF << "[warning]: trailing comments in IDF file (comments will be lost)\n";
1929
1930 return;
1931 }
1932
1933 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1934 "problems reading board section" ) );
1935 }
1936
1937 if( isComment )
1938 {
1939 comments.push_back( iline );
1940 continue;
1941 }
1942
1943 // This must be a header
1944 GetIDFString( iline, token, quoted, idx );
1945
1946 if( quoted )
1947 {
1948 ostringstream ostr;
1949
1950 ostr << "invalid IDF file\n";
1951 ostr << "* Violation of specification: quoted string where SECTION HEADER expected\n";
1952 ostr << "* line: '" << iline << "'";
1953 ostr << "* position: " << pos;
1954
1955 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
1956 }
1957
1958 if( CompareToken( ".BOARD_OUTLINE", token ) )
1959 {
1960 if( aBoardState != FILE_STATE::FILE_HEADER )
1961 {
1962 aBoardState = FILE_STATE::FILE_INVALID;
1963 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1964 "invalid IDF file\n"
1965 "* Violation of specification: no HEADER section" ) );
1966 }
1967
1968 m_boardOutline.readData( aBoardFile, iline, m_idfVer );
1969
1970 if( !comments.empty() )
1971 {
1972 std::list<std::string>::iterator its = comments.begin();
1973 std::list<std::string>::iterator ite = comments.end();
1974
1975 while( its != ite )
1976 {
1977 m_boardOutline.AddComment( *its );
1978 ++its;
1979 }
1980 }
1981
1982 aBoardState = FILE_STATE::FILE_OUTLINE;
1983 return;
1984 }
1985
1986 if( CompareToken( ".PANEL_OUTLINE", token ) )
1987 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "PANEL_OUTLINE not supported" ) );
1988
1989 if( CompareToken( ".OTHER_OUTLINE", token ) )
1990 {
1991 if( aBoardState != FILE_STATE::FILE_OUTLINE )
1992 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
1993 "invalid IDF file\n"
1994 "* Violation of specification: expecting .BOARD_OUTLINE, have .OTHER_OUTLINE" ) );
1995
1996 OTHER_OUTLINE* op = nullptr;
1997
1998 try
1999 {
2000 op = new OTHER_OUTLINE( this );
2001 }
2002 catch( std::bad_alloc& )
2003 {
2004 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2005 "could not create OTHER_OUTLINE object" ) );
2006 }
2007
2008 if( op == nullptr )
2009
2010 op->SetUnit( m_unit );
2011 op->readData( aBoardFile, iline, m_idfVer );
2012
2013 if( !comments.empty() )
2014 {
2015 std::list<std::string>::iterator its = comments.begin();
2016 std::list<std::string>::iterator ite = comments.end();
2017
2018 while( its != ite )
2019 {
2020 op->AddComment( *its );
2021 ++its;
2022 }
2023 }
2024
2025 if( m_otherOutlines.emplace( op->GetOutlineIdentifier(), op ).second == false )
2026 {
2027 ostringstream ostr;
2028 ostr << "invalid IDF file\n";
2029 ostr << "* Violation of specification. Non-unique ID in OTHER_OUTLINE '";
2030 ostr << op->GetOutlineIdentifier() << "'\n";
2031 ostr << "* line: '" << iline << "'\n";
2032 ostr << "* pos: " << pos;
2033 delete op;
2034
2035 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2036 }
2037
2038 return;
2039 }
2040
2041 if( CompareToken( ".ROUTE_OUTLINE", token ) )
2042 {
2043 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2044 {
2045 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2046 "invalid IDF file\n"
2047 "* Violation of specification: expecting .BOARD_OUTLINE, have .ROUTE_OUTLINE" ) );
2048 }
2049
2050 ROUTE_OUTLINE* op = nullptr;
2051
2052 try
2053 {
2054 op = new ROUTE_OUTLINE( this );
2055 }
2056 catch( std::bad_alloc& )
2057 {
2058 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2059 "could not create ROUTE_OUTLINE object" ) );
2060 }
2061
2062 op->SetUnit( m_unit );
2063 op->readData( aBoardFile, iline, m_idfVer );
2064
2065 if( !comments.empty() )
2066 {
2067 std::list<std::string>::iterator its = comments.begin();
2068 std::list<std::string>::iterator ite = comments.end();
2069
2070 while( its != ite )
2071 {
2072 op->AddComment( *its );
2073 ++its;
2074 }
2075 }
2076
2077 m_routeOutlines.push_back( op );
2078
2079 return;
2080 }
2081
2082 if( CompareToken( ".PLACE_OUTLINE", token ) )
2083 {
2084 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2085 {
2086 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2087 "invalid IDF file\n"
2088 "* Violation of specification: expecting .BOARD_OUTLINE, have .PLACE_OUTLINE" ) );
2089 }
2090
2091 PLACE_OUTLINE* op = nullptr;
2092
2093 try
2094 {
2095 op = new PLACE_OUTLINE( this );
2096 }
2097 catch( std::bad_alloc& )
2098 {
2099 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2100 "could not create PLACE_OUTLINE object" ) );
2101 }
2102
2103 op->SetUnit( m_unit );
2104 op->readData( aBoardFile, iline, m_idfVer );
2105
2106 if( !comments.empty() )
2107 {
2108 std::list<std::string>::iterator its = comments.begin();
2109 std::list<std::string>::iterator ite = comments.end();
2110
2111 while( its != ite )
2112 {
2113 op->AddComment( *its );
2114 ++its;
2115 }
2116 }
2117
2118 m_placeOutlines.push_back( op );
2119
2120 return;
2121 }
2122
2123 if( CompareToken( ".ROUTE_KEEPOUT", token ) )
2124 {
2125 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2126 {
2127 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2128 "invalid IDF file\n"
2129 "* Violation of specification: expecting .BOARD_OUTLINE, have .ROUTE_KEEPOUT" ) );
2130 }
2131
2132 ROUTE_KO_OUTLINE* op = nullptr;
2133
2134 try
2135 {
2136 op = new ROUTE_KO_OUTLINE( this );
2137 }
2138 catch( std::bad_alloc& )
2139 {
2140 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2141 "could not create ROUTE_KEEPOUT object" ) );
2142 }
2143
2144 op->SetUnit( m_unit );
2145 op->readData( aBoardFile, iline, m_idfVer );
2146
2147 if( !comments.empty() )
2148 {
2149 std::list<std::string>::iterator its = comments.begin();
2150 std::list<std::string>::iterator ite = comments.end();
2151
2152 while( its != ite )
2153 {
2154 op->AddComment( *its );
2155 ++its;
2156 }
2157 }
2158
2159 m_routeKeepoutOutlines.push_back( op );
2160
2161 return;
2162 }
2163
2164 if( CompareToken( ".VIA_KEEPOUT", token ) )
2165 {
2166 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2167 {
2168 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2169 "invalid IDF file\n"
2170 "* Violation of specification: expecting .BOARD_OUTLINE, have .VIA_KEEPOUT" ) );
2171 }
2172
2173 VIA_KO_OUTLINE* op = nullptr;
2174
2175 try
2176 {
2177 op = new VIA_KO_OUTLINE( this );
2178 }
2179 catch( std::bad_alloc& )
2180 {
2181 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "could not create VIA_KEEPOUT object" ) );
2182 }
2183
2184 op->SetUnit( m_unit );
2185 op->readData( aBoardFile, iline, m_idfVer );
2186
2187 if( !comments.empty() )
2188 {
2189 std::list<std::string>::iterator its = comments.begin();
2190 std::list<std::string>::iterator ite = comments.end();
2191
2192 while( its != ite )
2193 {
2194 op->AddComment( *its );
2195 ++its;
2196 }
2197 }
2198
2199 m_viaKeepoutOutlines.push_back( op );
2200
2201 return;
2202 }
2203
2204 if( CompareToken( ".PLACE_KEEPOUT", token ) )
2205 {
2206 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2207 {
2208 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2209 "invalid IDF file\n"
2210 "* Violation of specification: expecting .BOARD_OUTLINE, have .PLACE_KEEPOUT" ) );
2211 }
2212
2213 PLACE_KO_OUTLINE* op = nullptr;
2214 try
2215 {
2216 op = new PLACE_KO_OUTLINE( this );
2217 }
2218 catch( std::bad_alloc& )
2219 {
2220 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "could not create PLACE_KEEPOUT object" ) );
2221 }
2222
2223 op->SetUnit( m_unit );
2224 op->readData( aBoardFile, iline, m_idfVer );
2225
2226 if( !comments.empty() )
2227 {
2228 std::list<std::string>::iterator its = comments.begin();
2229 std::list<std::string>::iterator ite = comments.end();
2230
2231 while( its != ite )
2232 {
2233 op->AddComment( *its );
2234 ++its;
2235 }
2236 }
2237
2238 m_placeKeepoutOutlines.push_back( op );
2239
2240 return;
2241 }
2242
2243 if( CompareToken( ".PLACE_REGION", token ) )
2244 {
2245 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2246 {
2247 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2248 "invalid IDF file\n"
2249 "* Violation of specification: expecting .BOARD_OUTLINE, have .PLACE_REGION" ) );
2250 }
2251
2252 GROUP_OUTLINE* op = nullptr;
2253
2254 try
2255 {
2256 op = new GROUP_OUTLINE( this );
2257 }
2258 catch( std::bad_alloc& )
2259 {
2260 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "could not create PLACE_REGION object" ) );
2261 }
2262
2263 op->SetUnit( m_unit );
2264 op->readData( aBoardFile, iline, m_idfVer );
2265
2266 if( !comments.empty() )
2267 {
2268 std::list<std::string>::iterator its = comments.begin();
2269 std::list<std::string>::iterator ite = comments.end();
2270
2271 while( its != ite )
2272 {
2273 op->AddComment( *its );
2274 ++its;
2275 }
2276 }
2277
2278 m_groupOutlines.emplace( op->GetGroupName(), op );
2279
2280 return;
2281 }
2282
2283 if( CompareToken( ".DRILLED_HOLES", token ) )
2284 {
2285 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2286 {
2287 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2288 "invalid IDF file\n"
2289 "* Violation of specification: expecting .BOARD_OUTLINE, have .DRILLED_HOLES" ) );
2290 }
2291
2292 readBrdDrills( aBoardFile, aBoardState );
2293
2294 if( !comments.empty() )
2295 {
2296 std::list<std::string>::iterator its = comments.begin();
2297 std::list<std::string>::iterator ite = comments.end();
2298
2299 while( its != ite )
2300 {
2301 m_drillComments.push_back( *its );
2302 ++its;
2303 }
2304 }
2305
2306 return;
2307 }
2308
2309 if( CompareToken( ".NOTES", token ) )
2310 {
2311 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2312 {
2313 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2314 "invalid IDF file\n"
2315 "* Violation of specification: expecting .BOARD_OUTLINE, have .NOTES" ) );
2316 }
2317
2319 {
2320 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2321 "invalid IDFv2 file\n"
2322 "* Violation of specification: NOTES section not in specification" ) );
2323 }
2324
2325 readBrdNotes( aBoardFile, aBoardState );
2326
2327 if( !comments.empty() )
2328 {
2329 std::list<std::string>::iterator its = comments.begin();
2330 std::list<std::string>::iterator ite = comments.end();
2331
2332 while( its != ite )
2333 {
2334 m_noteComments.push_back( *its );
2335 ++its;
2336 }
2337 }
2338
2339 return;
2340 }
2341
2342 if( CompareToken( ".PLACEMENT", token ) )
2343 {
2344 if( aBoardState != FILE_STATE::FILE_OUTLINE )
2345 {
2346 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2347 "invalid IDF file\n"
2348 "* Violation of specification: expecting .BOARD_OUTLINE, have .PLACEMENT" ) );
2349 }
2350
2351 readBrdPlacement( aBoardFile, aBoardState, aNoSubstituteOutlines );
2352
2353 if( !comments.empty() )
2354 {
2355 std::list<std::string>::iterator its = comments.begin();
2356 std::list<std::string>::iterator ite = comments.end();
2357
2358 while( its != ite )
2359 {
2360 m_placeComments.push_back( *its );
2361 ++its;
2362 }
2363 }
2364
2365 return;
2366 }
2367 }
2368}
2369
2370
2371void IDF3_BOARD::readBoardFile( const std::string& aFileName, bool aNoSubstituteOutlines )
2372{
2373 OPEN_ISTREAM( brd, aFileName.c_str() );
2374
2375 try
2376 {
2377 brd.exceptions ( std::ios_base::badbit );
2378
2379 if( brd.fail() )
2380 {
2381 ostringstream ostr;
2382 ostr << "\n* could not open file: '" << aFileName << "'";
2383
2384 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2385 }
2386
2387 brd.imbue( std::locale( "C" ) );
2388 std::string iline; // the input line
2389 bool isComment; // true if a line just read in is a comment line
2390 std::streampos pos;
2392
2393 // note: as per IDFv3 specification:
2394 // "The Header section must be the first section in the file, the second
2395 // section must be the Outline section, and the last section must be the
2396 // Placement section. All other sections may be in any order."
2397
2398 // further notes: Except for the HEADER section, sections may be preceded by
2399 // comment lines which will be copied back out on write(). No comments may
2400 // be associated with the board file itself since the only logical location
2401 // for unambiguous association is at the end of the file, which is inconvenient
2402 // for large files.
2403
2404 readBrdHeader( brd, state );
2405
2406 // read the various sections
2407 while( state != FILE_STATE::FILE_PLACEMENT && brd.good() )
2408 readBrdSection( brd, state, aNoSubstituteOutlines );
2409
2410 if( !brd.good() )
2411 {
2412 // check if we have valid data
2413 if( brd.eof() && state >= FILE_STATE::FILE_OUTLINE && state < FILE_STATE::FILE_INVALID )
2414 {
2415 CLOSE_STREAM( brd );
2416 return;
2417 }
2418
2419 CLOSE_STREAM( brd );
2420 ostringstream ostr;
2421 ostr << "\n* empty IDF file: '" << aFileName << "'";
2422
2423 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2424 }
2425
2426 if( brd.good() && state == FILE_STATE::FILE_PLACEMENT )
2427 {
2428 // read in any trailing lines and report on ignored comments (minor fault)
2429 // and any non-comment item (non-compliance with IDFv3)
2430 while( brd.good() )
2431 {
2432 while( !FetchIDFLine( brd, iline, isComment, pos ) && brd.good() );
2433
2434 // normally this is a fault but we have all the data in accordance with specs
2435 if( ( !brd.good() && !brd.eof() ) || iline.empty() )
2436 break;
2437
2438 if( isComment )
2439 {
2440 ERROR_IDF << "[warning]: trailing comments after PLACEMENT\n";
2441 }
2442 else
2443 {
2444 ostringstream ostr;
2445 ostr << "\n* problems reading file: '" << aFileName << "'";
2446
2447 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2448 "invalid IDF file\n"
2449 "* Violation of specification: non-comment lines after PLACEMENT section" ) );
2450 }
2451 }
2452 }
2453 }
2454 catch( const std::exception& )
2455 {
2456 brd.exceptions ( std::ios_base::goodbit );
2457 CLOSE_STREAM( brd );
2458 throw;
2459 }
2460
2461 CLOSE_STREAM( brd );
2462}
2463
2464
2465void IDF3_BOARD::readLibSection( std::istream& aLibFile, FILE_STATE& aLibState,
2466 IDF3_BOARD* aBoard )
2467{
2468 if( aBoard == nullptr )
2469 {
2470 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2471 "\n* BUG: invoked with nullptr reference aBoard" ) );
2472 }
2473
2474 std::list< std::string > comments; // comments associated with a section
2475
2476 // Reads in .ELECTRICAL, .MECHANICAL or #COMMENTS
2477 std::string iline; // the input line
2478 bool isComment; // true if a line just read in is a comment line
2479 std::streampos pos;
2480 int idx = 0;
2481 bool quoted = false;
2482 std::string token;
2483 IDF3_COMP_OUTLINE* pout = nullptr;
2484
2485 try
2486 {
2487 pout = new IDF3_COMP_OUTLINE( this );
2488 }
2489 catch( std::bad_alloc& )
2490 {
2491 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "\n* memory allocation failure" ) );
2492 }
2493
2494 while( aLibFile.good() )
2495 {
2496 while( !FetchIDFLine( aLibFile, iline, isComment, pos ) && aLibFile.good() );
2497
2498 if( !aLibFile.good() && !aLibFile.eof() )
2499 {
2500 delete pout;
2501 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "problems reading library section" ) );
2502 }
2503
2504 // no data was read; this only happens at eof()
2505 if( iline.empty() )
2506 {
2507 delete pout;
2508 return;
2509 }
2510
2511 if( isComment )
2512 {
2513 comments.push_back( iline );
2514 continue;
2515 }
2516
2517 // This must be a header
2518 GetIDFString( iline, token, quoted, idx );
2519
2520 if( quoted )
2521 {
2522 ostringstream ostr;
2523 ostr << "invalid IDF library\n";
2524 ostr << "* Violation of specification: quoted string where .ELECTRICAL or .MECHANICAL expected\n";
2525 ostr << "* line: '" << iline << "'\n";
2526 ostr << "* pos: " << pos;
2527 delete pout;
2528
2529 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2530 }
2531
2532 if( CompareToken( ".ELECTRICAL", token ) || CompareToken( ".MECHANICAL", token ) )
2533 {
2534 pout->readData( aLibFile, token, m_idfVer );
2535
2536 if( !comments.empty() )
2537 {
2538 std::list<std::string>::iterator its = comments.begin();
2539 std::list<std::string>::iterator ite = comments.end();
2540
2541 while( its != ite )
2542 {
2543 pout->AddComment( *its );
2544 ++its;
2545 }
2546 }
2547
2548 IDF3_COMP_OUTLINE* cop = aBoard->GetComponentOutline( pout->GetUID() );
2549
2550 if( cop == nullptr )
2551 {
2552 m_componentOutlines.emplace( pout->GetUID(), pout );
2553 }
2554 else
2555 {
2556 if( MatchCompOutline( pout, cop ) )
2557 {
2558 delete pout;
2559 // everything is fine; the outlines are genuine duplicates
2560 return;
2561 }
2562
2563 ostringstream ostr;
2564 ostr << "invalid IDF library\n";
2565 ostr << "duplicate Component Outline: '" << pout->GetUID() << "'\n";
2566 ostr << "* Violation of specification: multiple outlines have the same GEOM and PART name\n";
2567 ostr << "* line: '" << iline << "'\n";
2568 ostr << "* pos: " << pos;
2569 delete pout;
2570
2571 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2572 }
2573
2574 return;
2575 }
2576 else
2577 {
2578 ostringstream ostr;
2579 ostr << "invalid IDF library\n";
2580 ostr << "* Expecting .ELECTRICAL or .MECHANICAL, got '" << token << "'\n";
2581 ostr << "* line: '" << iline << "'\n";
2582 ostr << "* pos: " << pos;
2583 delete pout;
2584
2585 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2586 }
2587 }
2588
2589 delete pout;
2590
2591 if( !aLibFile.eof() )
2592 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "problems reading IDF library file" ) );
2593}
2594
2595
2596// read the library HEADER
2597void IDF3_BOARD::readLibHeader( std::istream& aLibFile, FILE_STATE& aLibState )
2598{
2599 std::string iline; // the input line
2600 bool isComment; // true if a line just read in is a comment line
2601 std::streampos pos;
2602 int idx = 0;
2603 bool quoted = false;
2604 std::string token;
2605
2606 // RECORD 1: ".HEADER" must be the very first line
2607 while( !FetchIDFLine( aLibFile, iline, isComment, pos ) && aLibFile.good() );
2608
2609 if( !aLibFile.good() )
2610 {
2611 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2612 "invalid IDF library file\n"
2613 "* premature end of file (no HEADER)" ) );
2614 }
2615
2616 if( isComment )
2617 {
2618 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2619 "invalid IDF library file\n"
2620 "* Violation of specification: first line must be .HEADER" ) );
2621 }
2622
2623 if( !CompareToken( ".HEADER", iline ) )
2624 {
2625 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2626 "invalid IDF library file\n"
2627 "* Violation of specification:\n"
2628 "* first line must be .HEADER and have no quotes or trailing text" ) );
2629 }
2630
2631 // RECORD 2:
2632 // File Type [str]: LIBRARY_FILE
2633 // IDF Version Number [float]: must be 3.0
2634 // Source System [str]: ignored
2635 // Date [str]: ignored
2636 // Library File Version [int]: ignored
2637 while( !FetchIDFLine( aLibFile, iline, isComment, pos ) && aLibFile.good() );
2638
2639 if( !aLibFile.good() )
2640 {
2641 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2642 "invalid IDF library file\n"
2643 "* premature end of HEADER" ) );
2644 }
2645
2646 if( isComment )
2647 {
2648 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2649 "invalid IDF library file\n"
2650 "* Violation of specification: comment within .HEADER section" ) );
2651 }
2652
2653 idx = 0;
2654 GetIDFString( iline, token, quoted, idx );
2655
2656 if( quoted )
2657 {
2658 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2659 "invalid IDF library file\n"
2660 "* Violation of specification:\n"
2661 "* file Type in HEADER section must not be in quotes" ) );
2662 }
2663
2664 if( !CompareToken( "LIBRARY_FILE", token ) )
2665 {
2666 ostringstream ostr;
2667 ostr << "invalid IDF library\n";
2668 ostr << "* Expecting string: LIBRARY_FILE (got '" << token << "')\n";
2669
2670 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2671 }
2672
2673 if( !GetIDFString( iline, token, quoted, idx ) )
2674 {
2675 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2676 "invalid IDF library file\n"
2677 "* Violation of specification: HEADER section, RECORD 2: no FIELD 2" ) );
2678 }
2679
2680 if( quoted )
2681 {
2682 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2683 "invalid IDF library file\n"
2684 "* Violation of specification: IDF Version must not be in quotes" ) );
2685 }
2686
2687 if( !token.compare( "3.0" ) || !token.compare( "3." ) || !token.compare( "3" ) )
2688 {
2690 }
2691 else if( !token.compare( "2.0" ) || !token.compare( "2." ) || !token.compare( "2" ) )
2692 {
2694 }
2695 else
2696 {
2697 ostringstream ostr;
2698
2699 ostr << "unsupported IDF version\n";
2700 ostr << "* Expecting version to be a variant of '3.0', '2.0' (value: '" << token << "')\n";
2701
2702 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2703 }
2704
2705 if( !GetIDFString( iline, token, quoted, idx ) )
2706 {
2707 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2708 "invalid IDF library file\n"
2709 "* Violation of specification:\n"
2710 "* HEADER section, RECORD 2, FIELD 3: no Source System string" ) );
2711 }
2712
2713 m_libSource = token;
2714
2715 if( !GetIDFString( iline, token, quoted, idx ) )
2716 {
2717 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2718 "invalid IDF library file\n"
2719 "* Violation of specification:\n"
2720 "* HEADER section, RECORD 2, FIELD 4: no Date string" ) );
2721 }
2722
2723 m_libDate = token;
2724
2725 if( !GetIDFString( iline, token, quoted, idx ) )
2726 {
2727 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2728 "invalid IDF library file\n"
2729 "* Violation of specification:\n"
2730 "* HEADER section, RECORD 2, FIELD 5: no Board File Version number" ) );
2731 }
2732
2733 std::istringstream istr;
2734 istr.str( token );
2735
2736 istr >> m_libFileVersion;
2737
2738 if( istr.fail() )
2739 {
2740 ERROR_IDF << "invalid Library File Version in header\n";
2741 cerr << "* Setting default version of 1\n";
2742 m_libFileVersion = 1;
2743 }
2744
2745 if( quoted )
2746 {
2747 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2748 "invalid IDF library file\n"
2749 "* Violation of specification:\n"
2750 "* HEADER section, RECORD 2, FIELD 5: Library File Version must not be in quotes" ) );
2751 }
2752
2753 // RECORD 3:
2754 // .END_HEADER
2755 while( !FetchIDFLine( aLibFile, iline, isComment, pos ) && aLibFile.good() );
2756
2757 if( ( !aLibFile.good() && !aLibFile.eof() ) || iline.empty() )
2758 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, "problems reading library header, RECORD 3" ) );
2759
2760 if( isComment )
2761 {
2762 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__,
2763 "invalid IDF library file\n"
2764 "* Violation of specification: comment within .HEADER section" ) );
2765 }
2766
2767 if( !CompareToken( ".END_HEADER", iline ) )
2768 {
2769 ostringstream ostr;
2770 ostr << "invalid IDF header\n";
2771 ostr << "* Violation of specification: expected .END_HEADER (got '" << iline << "')\n";
2772
2773 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2774 }
2775
2776 aLibState = FILE_STATE::FILE_HEADER;
2777}
2778
2779
2780void IDF3_BOARD::readLibFile( const std::string& aFileName )
2781{
2782 OPEN_ISTREAM( lib, aFileName.c_str() );
2783
2784 try
2785 {
2786 lib.exceptions ( std::ios_base::badbit );
2787
2788 if( lib.fail() )
2789 {
2790 ostringstream ostr;
2791 ostr << "\n* could not open file: '" << aFileName << "'";
2792
2793 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2794 }
2795
2796 lib.imbue( std::locale( "C" ) );
2798
2799 readLibHeader( lib, state );
2800
2801 while( lib.good() ) readLibSection( lib, state, this );
2802 }
2803 catch( const std::exception& )
2804 {
2805 lib.exceptions ( std::ios_base::goodbit );
2806 CLOSE_STREAM( lib );
2807 throw;
2808 }
2809
2810 CLOSE_STREAM( lib );
2811}
2812
2813
2814bool IDF3_BOARD::ReadFile( const wxString& aFullFileName, bool aNoSubstituteOutlines )
2815{
2816 // 1. Check that the file extension is 'emn'
2817 // 2. Check if a file with extension 'emp' exists and read it
2818 // 3. Open the specified filename and read it
2819
2820 wxFileName brdname( aFullFileName );
2821 wxFileName libname( aFullFileName );
2822 wxString ext = brdname.GetExt();
2823
2824 if( !ext.Cmp( wxT( "EMN" ) ) )
2825 {
2826 libname.SetExt( wxT( "EMP" ) );
2827 }
2828 else if( !ext.Cmp( wxT( "emn" ) ) )
2829 {
2830 libname.SetExt( wxT( "emp" ) );
2831 }
2832 else
2833 {
2834 ostringstream ostr;
2835 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
2836 ostr << "* [INFO] invalid file name: '" << aFullFileName.ToUTF8() << "'";
2837
2838 m_errormsg = ostr.str();
2839 }
2840
2841
2842 brdname.SetExt( wxT( "emn" ) );
2843
2844 std::string bfname = TO_UTF8( aFullFileName );
2845
2846 if( !wxFileExists( bfname ) )
2847 {
2848 brdname.SetExt( wxT( "EMN" ) );
2849 libname.SetExt( wxT( "EMP" ) );
2850 }
2851
2852 try
2853 {
2854 if( !brdname.IsOk() )
2855 {
2856 ostringstream ostr;
2857 ostr << "\n* invalid file name: '" << aFullFileName.ToUTF8() << "'";
2858
2859 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2860 }
2861
2862 if( !brdname.FileExists() )
2863 {
2864 ostringstream ostr;
2865 ostr << "\n* no such file: '" << aFullFileName.ToUTF8() << "'";
2866
2867 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2868 }
2869
2870 if( !brdname.IsFileReadable() )
2871 {
2872 ostringstream ostr;
2873 ostr << "\n* cannot read file: '" << bfname << "'";
2874
2875 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2876 }
2877
2878 bfname = TO_UTF8( brdname.GetFullPath() );
2879 std::string lfname = TO_UTF8( libname.GetFullPath() );
2880
2881 if( !libname.FileExists() )
2882 {
2883 // NOTE: Since this is a common case we simply proceed
2884 // with the assumption that there is no library file;
2885 // however we print a message to inform the user.
2886 ERROR_IDF;
2887 cerr << "no associated library file (*.emp)\n";
2888 }
2889 else if( !libname.IsFileReadable() )
2890 {
2891 ostringstream ostr;
2892 ostr << "\n* cannot read library file: '" << lfname << "'";
2893
2894 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2895 }
2896 else
2897 {
2898 // read the library file before proceeding
2899 readLibFile( lfname );
2900 }
2901
2902 // read the board file
2903 readBoardFile( bfname, aNoSubstituteOutlines );
2904 }
2905 catch( const std::exception& e )
2906 {
2907 Clear();
2908 m_errormsg = e.what();
2909
2910 return false;
2911 }
2912
2913 return true;
2914}
2915
2916
2917bool IDF3_BOARD::writeLibFile( const std::string& aFileName )
2918{
2919 OPEN_OSTREAM( lib, aFileName.c_str() );
2920
2921 try
2922 {
2923 lib.exceptions( std::ios_base::failbit );
2924
2925 if( lib.fail() )
2926 {
2927 ostringstream ostr;
2928 ostr << "\n* could not open file: '" << aFileName << "'";
2929
2930 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2931 }
2932
2933 lib.imbue( std::locale( "C" ) );
2934 wxDateTime tdate( time( nullptr ) );
2935
2936 if( m_idfSource.empty() )
2937 m_idfSource = "KiCad-IDF Framework";
2938
2939 ostringstream fileDate;
2940 fileDate << setfill( '0' ) << setw(4) << tdate.GetYear();
2941 fileDate << "/" << setw(2) << tdate.GetMonth() << "/" << tdate.GetDay();
2942 fileDate << "." << tdate.GetHour() << ":" << tdate.GetMinute() << ":" << tdate.GetSecond();
2943 m_libDate = fileDate.str();
2944
2945 lib << ".HEADER\n";
2946 lib << "LIBRARY_FILE 3.0 \"Created by " << m_idfSource;
2947 lib << "\" " << m_libDate << " " << (++m_libFileVersion ) << "\n";
2948 lib << ".END_HEADER\n\n";
2949
2950 std::map<std::string, IDF3_COMP_OUTLINE*>::iterator its = m_componentOutlines.begin();
2951 std::map<std::string, IDF3_COMP_OUTLINE*>::iterator ite = m_componentOutlines.end();
2952
2953 while( its != ite )
2954 {
2955 its->second->writeData( lib );
2956 ++its;
2957 }
2958
2959 }
2960 catch( const std::exception& )
2961 {
2962 lib.exceptions( std::ios_base::goodbit );
2963 CLOSE_STREAM( lib );
2964 throw;
2965 }
2966
2967 CLOSE_STREAM( lib );
2968 return true;
2969}
2970
2971
2972void IDF3_BOARD::writeBoardFile( const std::string& aFileName )
2973{
2974 OPEN_OSTREAM( brd, aFileName.c_str() );
2975
2976 try
2977 {
2978 brd.exceptions( std::ostream::failbit );
2979
2980 if( brd.fail() )
2981 {
2982 ostringstream ostr;
2983 ostr << "\n* could not open file: '" << aFileName << "'";
2984
2985 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
2986 }
2987
2988 brd.imbue( std::locale( "C" ) );
2989 wxDateTime tdate( time( nullptr ) );
2990
2991 if( m_idfSource.empty() )
2992 m_idfSource = "KiCad-IDF Framework";
2993
2994 ostringstream fileDate;
2995 fileDate << setfill( '0' ) << setw(4) << tdate.GetYear();
2996 fileDate << "/" << setw(2) << tdate.GetMonth() << "/" << tdate.GetDay();
2997 fileDate << "." << tdate.GetHour() << ":" << tdate.GetMinute() << ":" << tdate.GetSecond();
2998 m_brdDate = fileDate.str();
2999
3000 brd << ".HEADER\n";
3001 brd << "BOARD_FILE 3.0 \"Created by " << m_idfSource;
3002 brd << "\" " << m_brdDate << " " << (++m_brdFileVersion ) << "\n";
3003
3004 if( m_boardName.empty() )
3005 brd << "\"BOARD WITH NO NAME\" ";
3006 else
3007 brd << "\"" << m_boardName << "\" ";
3008
3009 brd << setw(1) << setfill( ' ' );
3010
3012 brd << "MM\n";
3013 else
3014 brd << "THOU\n";
3015
3016 brd << ".END_HEADER\n\n";
3017
3018 // write the BOARD_OUTLINE
3019 m_boardOutline.writeData( brd );
3020
3021 // OTHER outlines
3022 do
3023 {
3024 std::map<std::string, OTHER_OUTLINE*>::iterator its = m_otherOutlines.begin();
3025 std::map<std::string, OTHER_OUTLINE*>::iterator ite = m_otherOutlines.end();
3026
3027 while(its != ite )
3028 {
3029 its->second->writeData( brd );
3030 ++its;
3031 }
3032
3033 } while( 0 );
3034
3035 // ROUTE outlines
3036 do
3037 {
3038 std::list<ROUTE_OUTLINE*>::iterator its = m_routeOutlines.begin();
3039 std::list<ROUTE_OUTLINE*>::iterator ite = m_routeOutlines.end();
3040
3041 while( its != ite )
3042 {
3043 (*its)->writeData( brd );
3044 ++its;
3045 }
3046
3047 } while( 0 );
3048
3049 // PLACEMENT outlines
3050 do
3051 {
3052 std::list<PLACE_OUTLINE*>::iterator its = m_placeOutlines.begin();
3053 std::list<PLACE_OUTLINE*>::iterator ite = m_placeOutlines.end();
3054
3055 while( its != ite )
3056 {
3057 (*its)->writeData( brd );
3058 ++its;
3059 }
3060
3061 } while( 0 );
3062
3063 // ROUTE KEEPOUT outlines
3064 do
3065 {
3066 std::list<ROUTE_KO_OUTLINE*>::iterator its = m_routeKeepoutOutlines.begin();
3067 std::list<ROUTE_KO_OUTLINE*>::iterator ite = m_routeKeepoutOutlines.end();
3068
3069 while( its != ite )
3070 {
3071 (*its)->writeData( brd );
3072 ++its;
3073 }
3074
3075 } while( 0 );
3076
3077 // VIA KEEPOUT outlines
3078 do
3079 {
3080 std::list<VIA_KO_OUTLINE*>::iterator its = m_viaKeepoutOutlines.begin();
3081 std::list<VIA_KO_OUTLINE*>::iterator ite = m_viaKeepoutOutlines.end();
3082
3083 while( its != ite )
3084 {
3085 (*its)->writeData( brd );
3086 ++its;
3087 }
3088
3089 } while( 0 );
3090
3091 // PLACE KEEPOUT outlines
3092 do
3093 {
3094 std::list<PLACE_KO_OUTLINE*>::iterator its = m_placeKeepoutOutlines.begin();
3095 std::list<PLACE_KO_OUTLINE*>::iterator ite = m_placeKeepoutOutlines.end();
3096
3097 while( its != ite )
3098 {
3099 (*its)->writeData( brd );
3100 ++its;
3101 }
3102
3103 } while( 0 );
3104
3105 // PLACEMENT GROUP outlines
3106 do
3107 {
3108 std::multimap<std::string, GROUP_OUTLINE*>::iterator its = m_groupOutlines.begin();
3109 std::multimap<std::string, GROUP_OUTLINE*>::iterator ite = m_groupOutlines.end();
3110
3111 while( its != ite )
3112 {
3113 its->second->writeData( brd );
3114 ++its;
3115 }
3116
3117 } while( 0 );
3118
3119 // Drilled holes
3120 do
3121 {
3122 std::list<std::string>::iterator itds = m_drillComments.begin();
3123 std::list<std::string>::iterator itde = m_drillComments.end();
3124
3125 while( itds != itde )
3126 {
3127 brd << "# " << *itds << "\n";
3128 ++itds;
3129 }
3130
3131 brd << ".DRILLED_HOLES\n";
3132
3133 std::list<IDF_DRILL_DATA*>::iterator itbs = m_drills.begin();
3134 std::list<IDF_DRILL_DATA*>::iterator itbe = m_drills.end();
3135
3136 while( itbs != itbe )
3137 {
3138 (*itbs)->write( brd, m_unit );
3139 ++itbs;
3140 }
3141
3142 std::map<std::string, IDF3_COMPONENT*>::iterator itcs = m_components.begin();
3143 std::map<std::string, IDF3_COMPONENT*>::iterator itce = m_components.end();
3144
3145 while( itcs != itce )
3146 {
3147 itcs->second->writeDrillData( brd );
3148 ++itcs;
3149 }
3150
3151 brd << ".END_DRILLED_HOLES\n\n";
3152 } while( 0 );
3153
3154 // Notes
3155 if( !m_notes.empty() )
3156 {
3157 std::list<std::string>::iterator itncs = m_noteComments.begin();
3158 std::list<std::string>::iterator itnce = m_noteComments.end();
3159
3160 while( itncs != itnce )
3161 {
3162 brd << "# " << *itncs << "\n";
3163 ++itncs;
3164 }
3165
3166 brd << ".NOTES\n";
3167
3168 std::list<IDF_NOTE*>::iterator itns = m_notes.begin();
3169 std::list<IDF_NOTE*>::iterator itne = m_notes.end();
3170
3171 while( itns != itne )
3172 {
3173 (*itns)->writeNote( brd, m_unit );
3174 ++itns;
3175 }
3176
3177 brd << ".END_NOTES\n\n";
3178
3179 }
3180
3181 // Placement
3182 if( !m_components.empty() )
3183 {
3184 std::list<std::string>::iterator itpcs = m_placeComments.begin();
3185 std::list<std::string>::iterator itpce = m_placeComments.end();
3186
3187 while( itpcs != itpce )
3188 {
3189 brd << "# " << *itpcs << "\n";
3190 ++itpcs;
3191 }
3192
3193 std::map< std::string, IDF3_COMPONENT*>::iterator itcs = m_components.begin();
3194 std::map< std::string, IDF3_COMPONENT*>::iterator itce = m_components.end();
3195
3196 // determine if there are any component outlines at all and avoid
3197 // writing an empty PLACEMENT section if there are no outlines.
3198 // this will cost a little time but prevents software such as
3199 // CircuitWorks from segfaulting on an empty section.
3200
3201 bool hasOutlines = false;
3202
3203 while( itcs != itce )
3204 {
3205 if( itcs->second->GetOutlinesSize() > 0 )
3206 {
3207 itcs = m_components.begin();
3208 hasOutlines = true;
3209 break;
3210 }
3211
3212 ++itcs;
3213 }
3214
3215 if( hasOutlines )
3216 {
3217 brd << ".PLACEMENT\n";
3218
3219 while( itcs != itce )
3220 {
3221 itcs->second->writePlaceData( brd );
3222 ++itcs;
3223 }
3224
3225 brd << ".END_PLACEMENT\n";
3226 }
3227
3228 }
3229
3230 }
3231 catch( const std::exception& )
3232 {
3233 brd.exceptions( std::ios_base::goodbit );
3234 CLOSE_STREAM( brd );
3235 throw;
3236 }
3237
3238 CLOSE_STREAM( brd );
3239}
3240
3241
3242bool IDF3_BOARD::WriteFile( const wxString& aFullFileName, IDF_UNIT aUnits, REPORTER* aReporter )
3243{
3244 setUnit( aUnits, false );
3245
3246 wxFileName brdname( aFullFileName );
3247 wxFileName libname( aFullFileName );
3248
3249 brdname.SetExt( FILEEXT::IdfV3BoardFileExtension );
3250 libname.SetExt( FILEEXT::IdfV3LibraryFileExtension );
3251
3252 std::string bfname = TO_UTF8( aFullFileName );
3253
3254 try
3255 {
3256 if( !brdname.IsOk() )
3257 {
3258 if( aReporter )
3259 {
3260 aReporter->Report( wxString::Format( _( "Invalid output path: %s" ), brdname.GetFullPath() ),
3262 }
3263
3264 return false;
3265 }
3266
3267 if( brdname.FileExists() && !brdname.IsFileWritable() )
3268 {
3269 if( aReporter )
3270 {
3271 aReporter->Report( wxString::Format( _( "Cannot overwrite file: %s" ), brdname.GetFullPath() ),
3273 }
3274
3275 return false;
3276 }
3277
3278 bfname = TO_UTF8( brdname.GetFullPath() );
3279 std::string lfname = TO_UTF8( libname.GetFullPath() );
3280
3281 if( libname.FileExists() && !libname.IsFileWritable() )
3282 {
3283 if( aReporter )
3284 {
3285 aReporter->Report( wxString::Format( _( "Cannot overwrite file: %s" ), libname.GetFullPath() ),
3287 }
3288
3289 return false;
3290 }
3291
3292 writeLibFile( lfname );
3293 writeBoardFile( bfname );
3294 }
3295 catch( const std::exception& e )
3296 {
3297 m_errormsg = e.what();
3298
3299 if( aReporter )
3300 aReporter->Report( wxString::Format( _( "Error writing IDF file: %s" ), e.what() ), RPT_SEVERITY_ERROR );
3301
3302 return false;
3303 }
3304
3305 return true;
3306}
3307
3308
3310{
3311 if( aVersion < 0 )
3312 {
3313 ostringstream ostr;
3314 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3315 ostr << "* board version (" << aVersion << ") must be >= 0";
3316 m_errormsg = ostr.str();
3317
3318 return false;
3319 }
3320
3321 m_brdFileVersion = aVersion;
3322
3323 return true;
3324}
3325
3326
3328{
3329 if( aVersion < 0 )
3330 {
3331 ostringstream ostr;
3332 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3333 ostr << "* library version (" << aVersion << ") must be >= 0";
3334 m_errormsg = ostr.str();
3335
3336 return false;
3337 }
3338
3339 m_libFileVersion = aVersion;
3340
3341 return true;
3342}
3343
3344
3345bool IDF3_BOARD::SetUserScale( double aScaleFactor )
3346{
3347 if( aScaleFactor == 0.0 )
3348 {
3349 ostringstream ostr;
3350 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3351 ostr << "* BUG: user scale factor must not be 0";
3352 m_errormsg = ostr.str();
3353
3354 return false;
3355 }
3356
3357 m_userScale = aScaleFactor;
3358 return true;
3359}
3360
3361
3362bool IDF3_BOARD::SetUserPrecision( int aPrecision )
3363{
3364 if( aPrecision < 1 || aPrecision > 8 )
3365 {
3366 ostringstream ostr;
3367 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3368 ostr << "* precision value (" << aPrecision << ") must be 1..8";
3369 m_errormsg = ostr.str();
3370
3371 return false;
3372 }
3373
3374 m_userPrec = aPrecision;
3375 return true;
3376}
3377
3378
3379void IDF3_BOARD::GetUserOffset( double& aXoff, double& aYoff )
3380{
3381 aXoff = m_userXoff;
3382 aYoff = m_userYoff;
3383}
3384
3385
3386void IDF3_BOARD::SetUserOffset( double aXoff, double aYoff )
3387{
3388 m_userXoff = aXoff;
3389 m_userYoff = aYoff;
3390}
3391
3392
3394{
3395 if( !m_boardOutline.AddOutline( aOutline ) )
3396 {
3397 m_errormsg = m_boardOutline.GetError();
3398
3399 return false;
3400 }
3401
3402 return true;
3403}
3404
3405
3407{
3408 if( !m_boardOutline.DelOutline( aOutline ) )
3409 {
3410 m_errormsg = m_boardOutline.GetError();
3411 return false;
3412 }
3413
3414 return true;
3415}
3416
3417
3418bool IDF3_BOARD::DelBoardOutline( size_t aIndex )
3419{
3420 if( !m_boardOutline.DelOutline( aIndex ) )
3421 {
3422 m_errormsg = m_boardOutline.GetError();
3423 return false;
3424 }
3425
3426 return true;
3427}
3428
3429
3431{
3432 return m_boardOutline.OutlinesSize();
3433}
3434
3435
3436IDF_DRILL_DATA* IDF3_BOARD::AddBoardDrill( double aDia, double aXpos, double aYpos,
3437 IDF3::KEY_PLATING aPlating,
3438 const std::string& aHoleType,
3439 IDF3::KEY_OWNER aOwner )
3440{
3441 IDF_DRILL_DATA* drill = nullptr;
3442
3443 try
3444 {
3445 drill = new IDF_DRILL_DATA( aDia, aXpos, aYpos, aPlating, "BOARD", aHoleType, aOwner );
3446 }
3447 catch( std::bad_alloc& )
3448 {
3449 return nullptr;
3450 }
3451
3452 m_drills.push_back( drill );
3453
3454 return drill;
3455}
3456
3457
3459{
3460 if( !aDrilledHole )
3461 return nullptr;
3462
3463 // note: PANEL drills are essentially BOARD drills which
3464 // the panel requires to be present
3465 if( CompareToken( "BOARD", aDrilledHole->GetDrillRefDes() )
3466 || CompareToken( "PANEL", aDrilledHole->GetDrillRefDes() ) )
3467 {
3468 m_drills.push_back( aDrilledHole );
3469 return aDrilledHole;
3470 }
3471
3472 return addCompDrill( aDrilledHole );
3473}
3474
3475
3476bool IDF3_BOARD::DelBoardDrill( double aDia, double aXpos, double aYpos )
3477{
3478 m_errormsg.clear();
3479
3480 std::list<IDF_DRILL_DATA*>::iterator sp = m_drills.begin();
3481 std::list<IDF_DRILL_DATA*>::iterator ep = m_drills.end();
3482 bool rval = false;
3483
3484 while( sp != ep )
3485 {
3486 if( (*sp)->Matches( aDia, aXpos, aYpos ) )
3487 {
3488#ifndef DISABLE_IDF_OWNERSHIP
3489 IDF3::KEY_OWNER keyo = (*sp)->GetDrillOwner();
3490
3491 if( keyo == KEY_OWNER::UNOWNED || ( keyo == KEY_OWNER::MCAD && m_cadType == CAD_TYPE::CAD_MECH )
3492 || ( keyo == KEY_OWNER::ECAD && m_cadType == CAD_TYPE::CAD_ELEC ) )
3493 {
3494 rval = true;
3495 delete *sp;
3496 sp = m_drills.erase( sp );
3497 continue;
3498 }
3499 else
3500 {
3501 ostringstream ostr;
3502 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3503 ostr << "* ownership violation; drill owner (";
3504
3505 switch( keyo )
3506 {
3507 case KEY_OWNER::ECAD:
3508 ostr << "ECAD";
3509 break;
3510
3511 case KEY_OWNER::MCAD:
3512 ostr << "MCAD";
3513 break;
3514
3515 default:
3516 ostr << "invalid: " << static_cast<int>( keyo );
3517 break;
3518 }
3519
3520 ostr << ") may not be modified by ";
3521
3523 ostr << "MCAD";
3524 else
3525 ostr << "ECAD";
3526
3527 m_errormsg = ostr.str();
3528
3529 ++sp;
3530 continue;
3531 }
3532#else
3533 rval = true;
3534 delete *sp;
3535 sp = m_drills.erase( sp );
3536 continue;
3537#endif
3538 }
3539
3540 ++sp;
3541 }
3542
3543 return rval;
3544}
3545
3546
3547bool IDF3_BOARD::AddSlot( double aWidth, double aLength, double aOrientation, double aX, double aY )
3548{
3549 if( aWidth < IDF_MIN_DIA_MM )
3550 {
3551 ostringstream ostr;
3552 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3553 ostr << "* slot width (" << aWidth << ") must be >= " << IDF_MIN_DIA_MM;
3554 m_errormsg = ostr.str();
3555
3556 return false;
3557 }
3558
3559 if( aLength < IDF_MIN_DIA_MM )
3560 {
3561 ostringstream ostr;
3562 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3563 ostr << "* slot length (" << aLength << ") must be >= " << IDF_MIN_DIA_MM;
3564 m_errormsg = ostr.str();
3565
3566 return false;
3567 }
3568
3569 IDF_POINT c[2]; // centers
3570 IDF_POINT pt[4];
3571
3572 double a1 = aOrientation / 180.0 * M_PI;
3573 double a2 = a1 + M_PI_2;
3574 double d1 = aLength / 2.0;
3575 double d2 = aWidth / 2.0;
3576 double sa1 = sin( a1 );
3577 double ca1 = cos( a1 );
3578 double dsa2 = d2 * sin( a2 );
3579 double dca2 = d2 * cos( a2 );
3580
3581 c[0].x = aX + d1 * ca1;
3582 c[0].y = aY + d1 * sa1;
3583
3584 c[1].x = aX - d1 * ca1;
3585 c[1].y = aY - d1 * sa1;
3586
3587 pt[0].x = c[0].x - dca2;
3588 pt[0].y = c[0].y - dsa2;
3589
3590 pt[1].x = c[1].x - dca2;
3591 pt[1].y = c[1].y - dsa2;
3592
3593 pt[2].x = c[1].x + dca2;
3594 pt[2].y = c[1].y + dsa2;
3595
3596 pt[3].x = c[0].x + dca2;
3597 pt[3].y = c[0].y + dsa2;
3598
3599 IDF_OUTLINE* outline = nullptr;
3600
3601 try
3602 {
3603 outline = new IDF_OUTLINE;
3604 }
3605 catch( std::bad_alloc& )
3606 {
3607 ostringstream ostr;
3608 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3609 ostr << "* could not create an outline object";
3610 m_errormsg = ostr.str();
3611
3612 return false;
3613 }
3614
3615 // first straight run
3616 IDF_SEGMENT* seg = new IDF_SEGMENT( pt[0], pt[1] );
3617 outline->push( seg );
3618
3619 // first 180 degree cap
3620 seg = new IDF_SEGMENT( c[1], pt[1], -180.0, true );
3621 outline->push( seg );
3622
3623 // final straight run
3624 seg = new IDF_SEGMENT( pt[2], pt[3] );
3625 outline->push( seg );
3626
3627 // final 180 degree cap
3628 seg = new IDF_SEGMENT( c[0], pt[3], -180.0, true );
3629 outline->push( seg );
3630
3631 if( !m_boardOutline.addOutline( outline ) )
3632 {
3633 m_errormsg = m_boardOutline.GetError();
3634 return false;
3635 }
3636
3637 return true;
3638}
3639
3640
3641IDF_DRILL_DATA* IDF3_BOARD::addCompDrill( double aDia, double aXpos, double aYpos,
3642 IDF3::KEY_PLATING aPlating,
3643 const std::string& aHoleType,
3644 IDF3::KEY_OWNER aOwner,
3645 const std::string& aRefDes )
3646{
3647 // first find the matching component; if it doesn't exist we must create it somehow -
3648 // question is, do we need a component outline at this stage or can those be added later?
3649 //
3650 // Presumably we can create a component with no outline and add the outlines later.
3651 // If a component is created and an outline specified but the outline is not loaded,
3652 // we're screwed if (a) we have already read the library file (*.emp) or (b) we don't
3653 // know the filename
3654
3655 std::string refdes = aRefDes;
3656
3657 // note: for components 'NOREFDES' would be assigned a Unique ID, but for holes
3658 // there is no way of associating the hole with the correct entity (if any)
3659 // so a hole added with "NOREFDES" goes to a generic component "NOREFDES"
3660 if( refdes.empty() )
3661 refdes = "NOREFDES";
3662
3663 // check if the target is BOARD or PANEL
3664 if( CompareToken( "BOARD", refdes ) )
3665 return AddBoardDrill( aDia, aXpos, aYpos, aPlating, aHoleType, aOwner );
3666
3667 if( CompareToken( "PANEL", refdes ) )
3668 {
3669 ostringstream ostr;
3670 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3671 ostr << "* PANEL data not supported";
3672 m_errormsg = ostr.str();
3673
3674 return nullptr;
3675 }
3676
3677 std::map<std::string, IDF3_COMPONENT*>::iterator ref = m_components.find( refdes );
3678
3679 if( ref == m_components.end() )
3680 {
3681 // create the item
3682 IDF3_COMPONENT* comp = nullptr;
3683 try
3684 {
3685 comp = new IDF3_COMPONENT( this );
3686 }
3687 catch( std::bad_alloc& )
3688 {
3689 ostringstream ostr;
3690 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3691 ostr << "* could not create new component object";
3692 m_errormsg = ostr.str();
3693
3694 return nullptr;
3695 }
3696
3697 comp->SetParent( this );
3698 comp->SetRefDes( refdes );
3699 ref = m_components.emplace( comp->GetRefDes(), comp ).first;
3700 }
3701
3702 // add the drill
3703 IDF_DRILL_DATA* dp = ref->second->AddDrill( aDia, aXpos, aYpos, aPlating, aHoleType, aOwner );
3704
3705 if( !dp )
3706 {
3707 m_errormsg = ref->second->GetError();
3708 return nullptr;
3709 }
3710
3711 return dp;
3712}
3713
3714
3716{
3717 if( !aDrilledHole )
3718 {
3719 ostringstream ostr;
3720 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): nullptr pointer";
3721 m_errormsg = ostr.str();
3722
3723 return nullptr;
3724 }
3725
3726 if( CompareToken( "PANEL", aDrilledHole->GetDrillRefDes() ) )
3727 {
3728 ostringstream ostr;
3729 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3730 ostr << "* PANEL data not supported";
3731 m_errormsg = ostr.str();
3732
3733 return nullptr;
3734 }
3735
3736 std::map<std::string, IDF3_COMPONENT*>::iterator ref = m_components.find( aDrilledHole->GetDrillRefDes() );
3737
3738 if( ref == m_components.end() )
3739 {
3740 // create the item
3742 try
3743 {
3744 comp = new IDF3_COMPONENT( this );
3745 }
3746 catch( std::bad_alloc& )
3747 {
3748 ostringstream ostr;
3749 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
3750 ostr << "* could not create new component object";
3751 m_errormsg = ostr.str();
3752
3753 return nullptr;
3754 }
3755
3756 comp->SetParent( this );
3757 comp->SetRefDes( aDrilledHole->GetDrillRefDes() );
3758 ref = m_components.emplace( comp->GetRefDes(), comp ).first;
3759 }
3760
3761 IDF_DRILL_DATA* dp = ref->second->AddDrill( aDrilledHole );
3762
3763 if( !dp )
3764 {
3765 m_errormsg = ref->second->GetError();
3766 return nullptr;
3767 }
3768
3769 return dp;
3770}
3771
3772
3773bool IDF3_BOARD::delCompDrill( double aDia, double aXpos, double aYpos, const std::string& aRefDes )
3774{
3775 m_errormsg.clear();
3776
3777 std::map<std::string, IDF3_COMPONENT*>::iterator ref = m_components.find( aRefDes );
3778
3779 if( ref == m_components.end() )
3780 return false;
3781
3782 if( !ref->second->DelDrill( aDia, aXpos, aYpos ) )
3783 {
3784 m_errormsg = ref->second->GetError();
3785 return false;
3786 }
3787
3788 return true;
3789}
3790
3791
3793{
3794 if( !aComponent )
3795 {
3796 ostringstream ostr;
3797 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__;
3798 ostr << "(): Invalid component pointer (nullptr)";
3799 m_errormsg = ostr.str();
3800
3801 return false;
3802 }
3803
3804 if( m_components.emplace( aComponent->GetRefDes(), aComponent ).second == false )
3805 {
3806 ostringstream ostr;
3807 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): \n";
3808 ostr << "* duplicate RefDes ('" << aComponent->GetRefDes() << "')";
3809 m_errormsg = ostr.str();
3810
3811 return false;
3812 }
3813
3814 return true;
3815}
3816
3817
3819{
3820 m_errormsg.clear();
3821
3822#ifndef DISABLE_IDF_OWNERSHIP
3823 if( !checkComponentOwnership( __LINE__, __FUNCTION__, aComponent ) )
3824 return false;
3825#endif
3826
3827 std::map<std::string, IDF3_COMPONENT*>::iterator it = m_components.find( aComponent->GetRefDes() );
3828
3829 if( it == m_components.end() )
3830 return false;
3831
3832 delete it->second;
3833 m_components.erase( it );
3834
3835 return true;
3836}
3837
3838
3839bool IDF3_BOARD::DelComponent( size_t aIndex )
3840{
3841 if( aIndex >= m_components.size() )
3842 {
3843 ostringstream ostr;
3844 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): \n";
3845 ostr << "* aIndex (" << aIndex << ") out of range (" << m_components.size() << ")";
3846 m_errormsg = ostr.str();
3847
3848 return false;
3849 }
3850
3851 std::map<std::string, IDF3_COMPONENT*>::iterator it = m_components.begin();
3852
3853 while( aIndex-- > 0 ) ++it;
3854
3855#ifndef DISABLE_IDF_OWNERSHIP
3856 if( !checkComponentOwnership( __LINE__, __FUNCTION__, it->second ) )
3857 return false;
3858#endif
3859
3860 delete it->second;
3861 m_components.erase( it );
3862
3863 return true;
3864}
3865
3866
3868{
3869 return m_components.size();
3870}
3871
3872
3873std::map< std::string, IDF3_COMPONENT* >* IDF3_BOARD::GetComponents( void )
3874{
3875 return &m_components;
3876}
3877
3878
3879IDF3_COMPONENT* IDF3_BOARD::FindComponent( const std::string& aRefDes )
3880{
3881 std::map<std::string, IDF3_COMPONENT*>::iterator it = m_components.find( aRefDes );
3882
3883 if( it == m_components.end() )
3884 return nullptr;
3885
3886 return it->second;
3887}
3888
3889
3891{
3892 std::string fname = TO_UTF8( aFullFileName );
3893 wxFileName idflib( aFullFileName );
3894
3895 if( !idflib.IsOk() )
3896 {
3897 ostringstream ostr;
3898 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): \n";
3899 cerr << "* invalid file name: '" << fname << "'";
3900 m_errormsg = ostr.str();
3901
3902 return nullptr;
3903 }
3904
3905 if( !idflib.FileExists() )
3906 {
3907 ostringstream ostr;
3908 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): \n";
3909 cerr << "* no such file: '" << fname << "'";
3910 m_errormsg = ostr.str();
3911
3912 return nullptr;
3913 }
3914
3915 if( !idflib.IsFileReadable() )
3916 {
3917 ostringstream ostr;
3918 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): \n";
3919 cerr << "* cannot read file: '" << fname << "'";
3920 m_errormsg = ostr.str();
3921
3922 return nullptr;
3923 }
3924
3925 std::map<std::string, std::string>::iterator itm = m_uidFileList.find( fname );
3926
3927 if( itm != m_uidFileList.end() )
3928 return GetComponentOutline( itm->second );
3929
3930 IDF3_COMP_OUTLINE* cp = nullptr;
3931
3932 try
3933 {
3934 cp = new IDF3_COMP_OUTLINE( this );
3935 }
3936 catch( std::bad_alloc& )
3937 {
3938 ostringstream ostr;
3939 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): \n";
3940 cerr << "* failed to create outline\n";
3941 cerr << "* filename: '" << fname << "'";
3942 m_errormsg = ostr.str();
3943
3944 return nullptr;
3945 }
3946
3947 OPEN_ISTREAM( model, fname.c_str() );
3948
3949 try
3950 {
3951 model.exceptions ( std::ios_base::badbit );
3952
3953 if( model.fail() )
3954 {
3955 ostringstream ostr;
3956 ostr << "\n* could not open file: '" << fname << "'";
3957
3958 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
3959 }
3960
3961 model.imbue( std::locale( "C" ) );
3962 std::string iline; // the input line
3963 bool isComment; // true if a line just read in is a comment line
3964 std::streampos pos;
3965
3966 while( true )
3967 {
3968 while( !FetchIDFLine( model, iline, isComment, pos ) && model.good() );
3969
3970 if( !model.good() )
3971 {
3972 ostringstream ostr;
3973 ostr << "\n* problems reading file: '" << fname << "'";
3974 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
3975 }
3976
3977 // accept comment lines, .ELECTRICAL, or .MECHANICAL only
3978 if( isComment )
3979 {
3980 cp->AddComment( iline );
3981 continue;
3982 }
3983
3984 if( CompareToken( ".ELECTRICAL", iline ) || CompareToken( ".MECHANICAL", iline ) )
3985 {
3986 cp->readData( model, iline, m_idfVer );
3987 break;
3988 }
3989 else
3990 {
3991 ostringstream ostr;
3992 ostr << "faulty IDF component definition\n";
3993 ostr << "* Expecting .ELECTRICAL or .MECHANICAL, got '" << iline << "'\n";
3994 cerr << "* File: '" << fname << "'\n";
3995
3996 throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) );
3997 }
3998 }
3999 }
4000 catch( const std::exception& e )
4001 {
4002 delete cp;
4003 model.exceptions ( std::ios_base::goodbit );
4005 m_errormsg = e.what();
4006 return nullptr;
4007 }
4008
4010
4011 // check the unique ID against the list from library components
4012 std::list<std::string>::iterator lsts = m_uidLibList.begin();
4013 std::list<std::string>::iterator lste = m_uidLibList.end();
4014 std::string uid = cp->GetUID();
4015 IDF3_COMP_OUTLINE* oldp = nullptr;
4016
4017 while( lsts != lste )
4018 {
4019 if( ! lsts->compare( uid ) )
4020 {
4021 oldp = GetComponentOutline( uid );
4022
4023 if( MatchCompOutline( cp, oldp ) )
4024 {
4025 // everything is fine; the outlines are genuine duplicates; delete the copy
4026 delete cp;
4027
4028 // make sure we can find the item via its filename
4029 m_uidFileList.emplace( fname, uid );
4030
4031 // return the pointer to the original
4032 return oldp;
4033 }
4034 else
4035 {
4036 delete cp;
4037 ostringstream ostr;
4038 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
4039 ostr << "* duplicate UID for different Component Outlines: '" << uid << "'\n";
4040 ostr << "* original loaded from library, duplicate in current file\n";
4041 ostr << "* file: '" << fname << "'";
4042
4043 m_errormsg = ostr.str();
4044 return nullptr;
4045 }
4046 }
4047
4048 ++lsts;
4049 }
4050
4051 // if we got this far then any duplicates are from files previously read
4052 oldp = GetComponentOutline( uid );
4053
4054 if( oldp == nullptr )
4055 {
4056 // everything is fine, there are no existing entries
4057 m_uidFileList.emplace( fname, uid );
4058 m_componentOutlines.emplace( uid, cp );
4059
4060 return cp;
4061 }
4062
4063 if( MatchCompOutline( cp, oldp ) )
4064 {
4065 // everything is fine; the outlines are genuine duplicates; delete the copy
4066 delete cp;
4067
4068 // make sure we can find the item via its other filename
4069 m_uidFileList.emplace( fname, uid );
4070
4071 // return the pointer to the original
4072 return oldp;
4073 }
4074
4075 delete cp;
4076
4077 // determine the file name of the first instance
4078 std::map<std::string, std::string>::iterator ufls = m_uidFileList.begin();
4079 std::map<std::string, std::string>::iterator ufle = m_uidFileList.end();
4080 std::string oldfname;
4081
4082 while( ufls != ufle )
4083 {
4084 if( ! ufls->second.compare( uid ) )
4085 {
4086 oldfname = ufls->first;
4087 break;
4088 }
4089
4090 ++ufls;
4091 }
4092
4093 ostringstream ostr;
4094 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "():\n";
4095 ostr << "* duplicate UID for different Component Outlines: '" << uid << "'\n";
4096 ostr << "* original file: '" << oldfname << "'\n";
4097 ostr << "* this file: '" << fname << "'";
4098
4099 m_errormsg = ostr.str();
4100 return nullptr;
4101}
4102
4103
4104IDF3_COMP_OUTLINE* IDF3_BOARD::GetComponentOutline( const std::string& aComponentID )
4105{
4106 std::map< std::string, IDF3_COMP_OUTLINE*>::iterator its = m_componentOutlines.find( aComponentID );
4107
4108 if( its != m_componentOutlines.end() )
4109 return its->second;
4110
4111 return nullptr;
4112}
4113
4114
4116 const std::string& aPartName )
4117{
4118 IDF3_COMP_OUTLINE* cp = GetComponentOutline( aGeomName + "_" + aPartName );
4119
4120 if( cp != nullptr )
4121 return cp;
4122
4123 try
4124 {
4125 cp = new IDF3_COMP_OUTLINE( this );
4126 }
4127 catch( std::bad_alloc& )
4128 {
4129 ostringstream ostr;
4130 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): \n";
4131 cerr << "could not create new outline";
4132 m_errormsg = ostr.str();
4133
4134 return nullptr;
4135 }
4136
4137 cp->SetGeomName( aGeomName );
4138 cp->SetPartName( aPartName );
4139 m_componentOutlines.emplace( cp->GetUID(), cp );
4140
4141 return cp;
4142}
4143
4144
4146 const std::string& aPartName )
4147{
4148 std::string uid;
4149 bool empty = false;
4150
4151 if( aGeomName.empty() && aPartName.empty() )
4152 {
4153 uid = "NOGEOM_NOPART";
4154 empty = true;
4155 }
4156 else
4157 {
4158 uid = aGeomName + "_" + aPartName;
4159 }
4160
4162
4163 if( cp != nullptr )
4164 return cp;
4165
4166 try
4167 {
4168 cp = new IDF3_COMP_OUTLINE( this );
4169 }
4170 catch( std::bad_alloc& )
4171 {
4172 ostringstream ostr;
4173 ostr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): ";
4174 cerr << "could not create new outline";
4175 m_errormsg = ostr.str();
4176
4177 return nullptr;
4178 }
4179
4180 if( empty )
4181 cp->CreateDefaultOutline( "", "" );
4182 else
4183 cp->CreateDefaultOutline( aGeomName, aPartName );
4184
4185 m_componentOutlines.emplace( cp->GetUID(), cp );
4186
4187 return cp;
4188}
4189
4190
4192{
4193 // preserve the board thickness
4194 double thickness = m_boardOutline.GetThickness();
4195
4196 m_idfSource.clear();
4197 m_brdSource.clear();
4198 m_libSource.clear();
4199 m_brdDate.clear();
4200 m_libDate.clear();
4201 m_uidFileList.clear();
4202 m_uidLibList.clear();
4203 m_brdFileVersion = 0;
4204 m_libFileVersion = 0;
4205 m_refDesCounter = 0;
4206 m_refDesString.clear();
4207
4208 // delete comment lists
4209 m_noteComments.clear();
4210 m_drillComments.clear();
4211 m_placeComments.clear();
4212
4213 // delete notes
4214 while( !m_notes.empty() )
4215 {
4216 delete m_notes.front();
4217 m_notes.pop_front();
4218 }
4219
4220 // delete drill list
4221 do
4222 {
4223 std::list<IDF_DRILL_DATA*>::iterator ds = m_drills.begin();
4224 std::list<IDF_DRILL_DATA*>::iterator de = m_drills.end();
4225
4226 while( ds != de )
4227 {
4228 delete *ds;
4229 ++ds;
4230 }
4231
4232 m_drills.clear();
4233 } while( 0 );
4234
4235
4236 // delete components
4237 do
4238 {
4239 std::map<std::string, IDF3_COMPONENT*>::iterator cs = m_components.begin();
4240 std::map<std::string, IDF3_COMPONENT*>::iterator ce = m_components.end();
4241
4242 while( cs != ce )
4243 {
4244 delete cs->second;
4245 ++cs;
4246 }
4247
4248 m_components.clear();
4249 } while( 0 );
4250
4251
4252 // delete component outlines
4253 do
4254 {
4255 std::map<std::string, IDF3_COMP_OUTLINE*>::iterator cs = m_componentOutlines.begin();
4256 std::map<std::string, IDF3_COMP_OUTLINE*>::iterator ce = m_componentOutlines.end();
4257
4258 while( cs != ce )
4259 {
4260 delete cs->second;
4261 ++cs;
4262 }
4263
4264 m_componentOutlines.clear();
4265 } while( 0 );
4266
4267
4268 // delete OTHER outlines
4269 do
4270 {
4271 std::map<std::string, OTHER_OUTLINE*>::iterator os = m_otherOutlines.begin();
4272 std::map<std::string, OTHER_OUTLINE*>::iterator oe = m_otherOutlines.end();
4273
4274 while( os != oe )
4275 {
4276 delete os->second;
4277 ++os;
4278 }
4279
4280 m_otherOutlines.clear();
4281 } while( 0 );
4282
4283
4284 // delete ROUTE outlines
4285 do
4286 {
4287 std::list<ROUTE_OUTLINE*>::iterator os = m_routeOutlines.begin();
4288 std::list<ROUTE_OUTLINE*>::iterator oe = m_routeOutlines.end();
4289
4290 while( os != oe )
4291 {
4292 delete *os;
4293 ++os;
4294 }
4295
4296 m_routeOutlines.clear();
4297 } while( 0 );
4298
4299
4300 // delete PLACE outlines
4301 do
4302 {
4303 std::list<PLACE_OUTLINE*>::iterator os = m_placeOutlines.begin();
4304 std::list<PLACE_OUTLINE*>::iterator oe = m_placeOutlines.end();
4305
4306 while( os != oe )
4307 {
4308 delete *os;
4309 ++os;
4310 }
4311
4312 m_placeOutlines.clear();
4313 } while( 0 );
4314
4315
4316 // delete ROUTE KEEPOUT outlines
4317 do
4318 {
4319 std::list<ROUTE_KO_OUTLINE*>::iterator os = m_routeKeepoutOutlines.begin();
4320 std::list<ROUTE_KO_OUTLINE*>::iterator oe = m_routeKeepoutOutlines.end();
4321
4322 while( os != oe )
4323 {
4324 delete *os;
4325 ++os;
4326 }
4327
4328 m_routeKeepoutOutlines.clear();
4329 } while( 0 );
4330
4331
4332 // delete VIA KEEPOUT outlines
4333 do
4334 {
4335 std::list<VIA_KO_OUTLINE*>::iterator os = m_viaKeepoutOutlines.begin();
4336 std::list<VIA_KO_OUTLINE*>::iterator oe = m_viaKeepoutOutlines.end();
4337
4338 while( os != oe )
4339 {
4340 delete *os;
4341 ++os;
4342 }
4343
4344 m_viaKeepoutOutlines.clear();
4345 } while( 0 );
4346
4347
4348 // delete PLACEMENT KEEPOUT outlines
4349 do
4350 {
4351 std::list<PLACE_KO_OUTLINE*>::iterator os = m_placeKeepoutOutlines.begin();
4352 std::list<PLACE_KO_OUTLINE*>::iterator oe = m_placeKeepoutOutlines.end();
4353
4354 while( os != oe )
4355 {
4356 delete *os;
4357 ++os;
4358 }
4359
4360 m_placeKeepoutOutlines.clear();
4361 } while( 0 );
4362
4363
4364 // delete PLACEMENT GROUP outlines
4365 do
4366 {
4367 std::multimap<std::string, GROUP_OUTLINE*>::iterator os = m_groupOutlines.begin();
4368 std::multimap<std::string, GROUP_OUTLINE*>::iterator oe = m_groupOutlines.end();
4369
4370 while( os != oe )
4371 {
4372 delete os->second;
4373 ++os;
4374 }
4375
4376 m_groupOutlines.clear();
4377 } while( 0 );
4378
4379 m_boardName.clear();
4380 m_boardOutline.setThickness( thickness );
4381
4383 m_userScale = 1.0;
4384 m_userXoff = 0.0;
4385 m_userYoff = 0.0;
4386}
IDF_OUTLINE * GetOutline(size_t aIndex)
Return a pointer to the outline as specified by aIndex.
size_t OutlinesSize(void)
virtual bool SetUnit(IDF3::IDF_UNIT aUnit)
Function SetUnit sets the native unit of the outline; except for component outlines this must be the ...
void AddComment(const std::string &aComment)
Add a comment to the outline data.
Regions and layers in which user-specified features or components may be placed.
virtual void readData(std::istream &aBoardFile, const std::string &aHeader, IDF3::IDF_VERSION aIdfVersion) override
Read GROUP_OUTLINE data from an open IDFv3 file.
virtual const std::string & GetGroupName(void)
Return a reference to the (non-unique) group name.
void GetUserOffset(double &aXoff, double &aYoff)
bool WriteFile(const wxString &aFullFileName, IDF3::IDF_UNIT aUnits, REPORTER *aReporter=nullptr)
std::list< std::string > m_uidLibList
Definition idf_parser.h:640
std::list< IDF_DRILL_DATA * > m_drills
Definition idf_parser.h:648
void readLibFile(const std::string &aFileName)
void readLibSection(std::istream &aLibFile, IDF3::FILE_STATE &aLibState, IDF3_BOARD *aBoard)
std::list< VIA_KO_OUTLINE * > m_viaKeepoutOutlines
Definition idf_parser.h:680
bool SetBoardThickness(double aBoardThickness)
IDF3::CAD_TYPE m_cadType
Definition idf_parser.h:653
int m_brdFileVersion
Definition idf_parser.h:665
bool SetLibraryVersion(int aVersion)
std::map< std::string, OTHER_OUTLINE * > m_otherOutlines
Definition idf_parser.h:674
bool setUnit(IDF3::IDF_UNIT aUnit, bool convert=false)
std::list< std::string > m_placeComments
Definition idf_parser.h:647
IDF3_COMP_OUTLINE * GetComponentOutline(const wxString &aFullFileName)
const std::string & GetNewRefDes(void)
std::string m_brdSource
Definition idf_parser.h:661
std::map< std::string, IDF3_COMP_OUTLINE * > m_componentOutlines
Definition idf_parser.h:650
std::list< IDF_NOTE * > m_notes
Definition idf_parser.h:644
bool writeLibFile(const std::string &aFileName)
void SetUserOffset(double aXoff, double aYoff)
int m_libFileVersion
Definition idf_parser.h:666
void readBrdNotes(std::istream &aBoardFile, IDF3::FILE_STATE &aBoardState)
double m_userXoff
Definition idf_parser.h:670
std::map< std::string, std::string > m_uidFileList
Definition idf_parser.h:639
std::list< PLACE_OUTLINE * > m_placeOutlines
Definition idf_parser.h:676
double m_userYoff
Definition idf_parser.h:671
std::string m_brdDate
Definition idf_parser.h:663
std::list< ROUTE_KO_OUTLINE * > m_routeKeepoutOutlines
Definition idf_parser.h:679
IDF3::CAD_TYPE GetCadType(void)
Definition idf_parser.h:425
int m_refDesCounter
Definition idf_parser.h:657
std::string m_refDesString
Definition idf_parser.h:658
IDF3::IDF_UNIT m_unit
Definition idf_parser.h:654
std::multimap< std::string, GROUP_OUTLINE * > m_groupOutlines
Definition idf_parser.h:677
bool DelBoardOutline(IDF_OUTLINE *aOutline)
IDF_DRILL_DATA * AddBoardDrill(double aDia, double aXpos, double aYpos, IDF3::KEY_PLATING aPlating, const std::string &aHoleType, IDF3::KEY_OWNER aOwner)
IDF3::IDF_VERSION m_idfVer
Definition idf_parser.h:655
std::string m_libSource
Definition idf_parser.h:662
bool ReadFile(const wxString &aFullFileName, bool aNoSubstituteOutlines=false)
bool delCompDrill(double aDia, double aXpos, double aYpos, const std::string &aRefDes)
void writeBoardFile(const std::string &aFileName)
std::map< std::string, IDF3_COMPONENT * > * GetComponents(void)
IDF3_COMPONENT * FindComponent(const std::string &aRefDes)
std::string m_boardName
Definition idf_parser.h:652
std::string m_errormsg
Definition idf_parser.h:643
IDF_DRILL_DATA * AddDrill(IDF_DRILL_DATA *aDrilledHole)
void readBrdHeader(std::istream &aBoardFile, IDF3::FILE_STATE &aBoardState)
size_t GetComponentsSize(void)
double GetBoardThickness(void)
void readLibHeader(std::istream &aLibFile, IDF3::FILE_STATE &aLibState)
virtual ~IDF3_BOARD()
std::string m_libDate
Definition idf_parser.h:664
bool AddComponent(IDF3_COMPONENT *aComponent)
void readBrdDrills(std::istream &aBoardFile, IDF3::FILE_STATE &aBoardState)
size_t GetBoardOutlinesSize(void)
bool SetUserScale(double aScaleFactor)
std::list< ROUTE_OUTLINE * > m_routeOutlines
Definition idf_parser.h:675
bool checkComponentOwnership(int aSourceLine, const char *aSourceFunc, IDF3_COMPONENT *aComponent)
void readBoardFile(const std::string &aFileName, bool aNoSubstituteOutlines)
void readBrdPlacement(std::istream &aBoardFile, IDF3::FILE_STATE &aBoardState, bool aNoSubstituteOutlines)
double m_userScale
Definition idf_parser.h:669
void readBrdSection(std::istream &aBoardFile, IDF3::FILE_STATE &aBoardState, bool aNoSubstituteOutlines)
IDF3::IDF_UNIT GetUnit(void) const
Definition idf_parser.h:432
bool SetUserPrecision(int aPrecision)
std::list< PLACE_KO_OUTLINE * > m_placeKeepoutOutlines
Definition idf_parser.h:681
void Clear(void)
std::string m_idfSource
Definition idf_parser.h:660
bool DelBoardDrill(double aDia, double aXpos, double aYpos)
BOARD_OUTLINE m_boardOutline
Definition idf_parser.h:673
IDF3_BOARD(IDF3::CAD_TYPE aCadType)
IDF3_COMP_OUTLINE * GetInvalidOutline(const std::string &aGeomName, const std::string &aPartName)
std::list< std::string > m_drillComments
Definition idf_parser.h:646
bool DelComponent(IDF3_COMPONENT *aComponent)
bool AddSlot(double aWidth, double aLength, double aOrientation, double aX, double aY)
bool SetBoardVersion(int aVersion)
bool AddBoardOutline(IDF_OUTLINE *aOutline)
std::map< std::string, IDF3_COMPONENT * > m_components
Definition idf_parser.h:649
IDF_DRILL_DATA * addCompDrill(double aDia, double aXpos, double aYpos, IDF3::KEY_PLATING aPlating, const std::string &aHoleType, IDF3::KEY_OWNER aOwner, const std::string &aRefDes)
std::list< std::string > m_noteComments
Definition idf_parser.h:645
const std::list< IDF3_COMP_OUTLINE_DATA * > * GetOutlinesData(void)
friend class IDF3_BOARD
Definition idf_parser.h:382
IDF3::IDF_PLACEMENT placement
Definition idf_parser.h:410
const std::string & GetRefDes(void)
Retrieve the Reference Designator (RefDes) of this component.
bool SetPlacement(IDF3::IDF_PLACEMENT aPlacementValue)
Set the placement value of the component subject to ownership rules.
IDF3_BOARD * parent
Definition idf_parser.h:414
bool writeDrillData(std::ostream &aBoardFile)
Write the internal drill data to an IDFv3 .DRILLED_HOLES section.
IDF3::IDF_LAYER layer
Definition idf_parser.h:411
bool DeleteOutlineData(IDF3_COMP_OUTLINE_DATA *aComponentOutline)
Remove outline data based on the pointer provided.
std::list< IDF_DRILL_DATA * > drills
Definition idf_parser.h:405
IDF3_COMPONENT(IDF3_BOARD *aParent)
Set the parent object and initializes other internal parameters to default values.
bool SetPosition(double aXpos, double aYpos, double aAngle, IDF3::IDF_LAYER aLayer)
Set the internal position parameters and returns true if the position was set, false if the position ...
std::list< IDF3_COMP_OUTLINE_DATA * > components
Definition idf_parser.h:404
const std::list< IDF_DRILL_DATA * > * GetDrills(void)
Return the internal list of drills.
bool SetRefDes(const std::string &aRefDes)
Set the Reference Designator (RefDes) of this component.
void SetParent(IDF3_BOARD *aParent)
Set the parent object.
IDF3::IDF_UNIT GetUnit(void)
IDF3::CAD_TYPE GetCadType(void)
bool hasPosition
True after SetPosition is called once.
Definition idf_parser.h:412
std::string errormsg
Definition idf_parser.h:415
bool checkOwnership(int aSourceLine, const char *aSourceFunc)
bool writePlaceData(std::ostream &aBoardFile)
Write the component placement data to an IDFv3 .PLACEMENT section.
IDF_DRILL_DATA * AddDrill(double aDia, double aXpos, double aYpos, IDF3::KEY_PLATING aPlating, const std::string &aHoleType, IDF3::KEY_OWNER aOwner)
Add a drill entry to the component and returns its pointer.
std::string refdes
Reference Description (MUST BE UNIQUE)
Definition idf_parser.h:413
size_t GetOutlinesSize(void)
bool GetPosition(double &aXpos, double &aYpos, double &aAngle, IDF3::IDF_LAYER &aLayer)
Retrieve the internal position parameters and returns true if the position was previously set,...
bool DelDrill(double aDia, double aXpos, double aYpos)
Delete a drill entry based on its size and location.
IDF3::IDF_PLACEMENT GetPlacement(void)
bool AddOutlineData(IDF3_COMP_OUTLINE_DATA *aComponentOutline)
Add the given component outline data to this component.
bool checkOwnership(int aSourceLine, const char *aSourceFunc)
bool readPlaceData(std::istream &aBoardFile, IDF3::FILE_STATE &aBoardState, IDF3_BOARD *aBoard, IDF3::IDF_VERSION aIdfVersion, bool aNoSubstituteOutlines)
Read placement data from an open IDFv3 file.
friend class IDF3_BOARD
Definition idf_parser.h:183
void GetOffsets(double &aXoff, double &aYoff, double &aZoff, double &aAngleOff)
Retrieve the position and orientation of this outline item in relation to its parent.
IDF3_COMP_OUTLINE * outline
Definition idf_parser.h:192
void writePlaceData(std::ostream &aBoardFile, double aXpos, double aYpos, double aAngle, const std::string &aRefDes, IDF3::IDF_PLACEMENT aPlacement, IDF3::IDF_LAYER aSide)
Write RECORD 2 and RECORD 3 of a PLACEMENT section as per IDFv3 specification.
IDF3_COMP_OUTLINE_DATA()
Create an object with default settings and no parent or associated outline.
void SetParent(IDF3_COMPONENT *aParent)
Set the parent object.
bool SetOutline(IDF3_COMP_OUTLINE *aOutline)
Set the outline whose position is managed by this object.
IDF3_COMPONENT * parent
Definition idf_parser.h:193
bool SetOffsets(double aXoff, double aYoff, double aZoff, double aAngleOff)
Set the position and orientation of this outline item in relation to its parent.
friend class IDF3_COMPONENT
Definition idf_parser.h:184
A component's outline as stored in an IDF library file.
void SetPartName(const std::string &aPartName)
Set the Part Name (Part Number, IDFv3 spec) of the component outline.
const std::string & GetUID(void)
virtual void readData(std::istream &aLibFile, const std::string &aHeader, IDF3::IDF_VERSION aIdfVersion) override
Read a component outline from an open IDFv3 file.
IDF3::COMP_TYPE GetComponentClass(void)
bool CreateDefaultOutline(const std::string &aGeom, const std::string &aPart)
Create a default outline with the given Geometry and Part names.
void SetGeomName(const std::string &aGeomName)
Set the Geometry Name (Package Name, IDFv3 spec) of the component outline.
int incrementRef(void)
Increment the internal reference counter to keep track of the number of components referring to this ...
A drilled hole.
Definition idf_common.h:312
const std::string & GetDrillRefDes()
bool read(std::istream &aBoardFile, IDF3::IDF_UNIT aBoardUnit, IDF3::FILE_STATE aBoardState, IDF3::IDF_VERSION aIdfVersion)
Read a drill entry from an IDFv3 file.
An entry in the NOTE section of an IDF file.
Definition idf_common.h:238
bool readNote(std::istream &aBoardFile, IDF3::FILE_STATE &aBoardState, IDF3::IDF_UNIT aBoardUnit)
Read a note entry from an IDFv3 file.
A segment and winding information for an IDF outline.
Definition idf_common.h:551
std::list< IDF_SEGMENT * >::iterator end()
Definition idf_common.h:623
std::list< IDF_SEGMENT * >::iterator begin()
Definition idf_common.h:615
size_t size()
Definition idf_common.h:583
bool push(IDF_SEGMENT *item)
Add a segment to the internal segment list.
A point as used by the various IDF related classes.
Definition idf_common.h:426
double x
Definition idf_common.h:457
double y
Definition idf_common.h:458
A segment as used in IDFv3 outlines.
Definition idf_common.h:468
Miscellaneous extrusions on the board.
virtual void readData(std::istream &aBoardFile, const std::string &aHeader, IDF3::IDF_VERSION aIdfVersion) override
Read an OTHER_OUTLINE data from an IDFv3 file.
virtual const std::string & GetOutlineIdentifier(void)
Function GetOutlineIdentifier returns the object's Outline Identifier.
Regions and layers in which no component may be placed or on which a maximum component height is in e...
Area on the board for placing components.
virtual void readData(std::istream &aBoardFile, const std::string &aHeader, IDF3::IDF_VERSION aIdfVersion) override
Read PLACE_OUTLINE data from an open IDFv3 file.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Regions and layers where no electrical routing is permitted.
Routing areas on the board.
virtual void readData(std::istream &aBoardFile, const std::string &aHeader, IDF3::IDF_VERSION aIdfVersion) override
Read ROUTE_OUTLINE data from an IDFv3 file.
Region in which vias are prohibited.
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
static const std::string IdfV3LibraryFileExtension
static const std::string IdfV3BoardFileExtension
#define IDF_THOU_TO_MM
Definition idf_helpers.h:37
#define IDF_MIN_DIA_MM
Definition idf_helpers.h:32
#define ERROR_IDF
Definition idf_helpers.h:29
static bool MatchCompOutline(IDF3_COMP_OUTLINE *aOutlineA, IDF3_COMP_OUTLINE *aOutlineB)
std::string GetPlacementString(IDF3::IDF_PLACEMENT aPlacement)
Return a string representing the given IDF Placement type.
FILE_STATE
State values for the IDF parser's input.
Definition idf_common.h:63
KEY_PLATING
The plating condition of a hole.
Definition idf_common.h:107
IDF_PLACEMENT
The placement status of a component.
Definition idf_common.h:188
bool GetIDFString(const std::string &aLine, std::string &aIDFString, bool &hasQuotes, int &aIndex)
Parse a line retrieved via FetchIDFLine() and returns the first IDF string found from the starting po...
CAD_TYPE
The class of CAD program which is opening or modifying a file.
Definition idf_common.h:127
bool FetchIDFLine(std::istream &aModel, std::string &aLine, bool &isComment, std::streampos &aFilePos)
Retrieve a single line from an IDF file and performs minimal processing.
IDF_VERSION
The supported IDF versions (3.0 and 2.0 ONLY).
Definition idf_common.h:76
IDF_UNIT
The native unit of the board and of component outlines.
Definition idf_common.h:177
bool CompareToken(const char *aTokenString, const std::string &aInputString)
Perform a case-insensitive comparison of a token string and an input string.
IDF_LAYER
The various IDF layer classes and groupings.
Definition idf_common.h:137
KEY_OWNER
The type of CAD which has ownership an object.
Definition idf_common.h:85
bool WriteLayersText(std::ostream &aBoardFile, IDF3::IDF_LAYER aLayer)
Write out text corresponding to the given IDF Layer type.
std::string GetLayerString(IDF3::IDF_LAYER aLayer)
Return a string representing the given IDF Layer type.
STL namespace.
@ RPT_SEVERITY_ERROR
#define OPEN_OSTREAM(var, name)
#define CLOSE_STREAM(var)
#define OPEN_ISTREAM(var, name)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
struct DROP_TABLE cs
KIBIS_MODEL * model
KIBIS_COMPONENT * comp
#define M_PI
Definition of file extensions used in Kicad.