sch_component.h 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

25 26 27 28
/**
 * @file sch_component.h
 * @brief Definition the SCH_COMPONENT class for Eeschema.
 */
plyatov's avatar
plyatov committed
29 30 31 32 33

#ifndef COMPONENT_CLASS_H
#define COMPONENT_CLASS_H


34 35 36
#include <sch_field.h>
#include <transform.h>
#include <general.h>
37

38

39
class SCH_SHEET_PATH;
40
class LIB_ITEM;
41 42
class LIB_PIN;
class LIB_COMPONENT;
43
class NETLIST_OBJECT_LIST;
44

45
/// A container for several SCH_FIELD items
46
typedef std::vector<SCH_FIELD> SCH_FIELDS;
47 48


49 50 51
/**
 * Class SCH_COMPONENT
 * describes a real schematic component
52
 */
53
class SCH_COMPONENT : public SCH_ITEM
plyatov's avatar
plyatov committed
54
{
55 56
    friend class DIALOG_EDIT_COMPONENT_IN_SCHEMATIC;

57
    wxPoint m_Pos;
58 59 60 61 62 63 64 65 66 67 68
    wxString m_ChipName;    ///< Name to look for in the library, i.e. "74LS00".
    int      m_unit;        ///< The unit for multiple part per package components.
    int      m_convert;     ///< The alternate body style for components that have more than
                            ///< one body style defined.  Primarily used for components that
                            ///< have a De Morgan conversion.
    wxString m_prefix;      ///< C, R, U, Q etc - the first character which typically indicates
                            ///< what the component is. Determined, upon placement, from the
                            ///< library component.  Created upon file load, by the first
                            ///<  non-digits in the reference fields.
    TRANSFORM m_transform;  ///< The rotation/mirror transformation matrix.
    SCH_FIELDS m_Fields;    ///< Variable length list of fields.
69

70 71 72 73 74 75 76 77
    /**
     * A temporary sheet path is required to generate the correct reference designator string
     * in complex heirarchies.  Hopefully this is only a temporary hack to decouple schematic
     * objects from the drawing window until a better design for handling complex heirarchies
     * can be implemented.
     */
    const SCH_SHEET_PATH* m_currentSheetPath;

78
    /**
79 80
     * Defines the hierarchical path and reference of the component.  This allows support
     * for hierarchical sheets that reference the same schematic.  The format for the path
81 82
     * is /&ltsheet time stamp&gt/&ltsheet time stamp&gt/.../&lscomponent time stamp&gt.
     * A single / denotes the root sheet.
charras's avatar
charras committed
83
     */
84
    wxArrayString m_PathsAndReferences;
85

86 87
    void Init( const wxPoint& pos = wxPoint( 0, 0 ) );

88
    EDA_RECT GetBodyBoundingBox() const;
89

plyatov's avatar
plyatov committed
90
public:
91
    SCH_COMPONENT( const wxPoint& pos = wxPoint( 0, 0 ), SCH_ITEM* aParent = NULL );
92

93 94 95 96 97
    /**
     * Create schematic component from library component object.
     *
     * @param libComponent - Component library object to create schematic
     *                       component from.
98
     * @param sheet - Schematic sheet the component is place into.
99 100 101 102 103 104 105
     * @param unit - Part for components that have multiple parts per
     *               package.
     * @param convert - Use the alternate body style for the schematic
     *                  component.
     * @param pos - Position to place new component.
     * @param setNewItemFlag - Set the component IS_NEW and IS_MOVED flags.
     */
106
    SCH_COMPONENT( LIB_COMPONENT& libComponent, SCH_SHEET_PATH* sheet,
107 108 109 110
                   int unit = 0, int convert = 0,
                   const wxPoint& pos = wxPoint( 0, 0 ),
                   bool setNewItemFlag = false );

111 112
    /**
     * Copy Constructor
113
     * clones \a aComponent into a new object.  All fields are copied as is except
114
     * for the linked list management pointers which are set to NULL, and the
115
     * SCH_FIELD's m_Parent pointers which are set to the new parent,
116 117
     * i.e. this new object.
     */
118
    SCH_COMPONENT( const SCH_COMPONENT& aComponent );
119

120
    ~SCH_COMPONENT() { }
CHARRAS's avatar
CHARRAS committed
121

122
    wxString GetClass() const
123
    {
124
        return wxT( "SCH_COMPONENT" );
125
    }
126

127 128 129 130 131 132
    wxString GetLibName() const { return m_ChipName; }

    void SetLibName( const wxString& aName );

    int GetUnit() const { return m_unit; }

133 134 135 136 137 138
    /**
     * change the unit id to aUnit
     * has maening only for multiple parts per package
     * Also set the modified flag bit
     * @param aUnit = the new unit Id
     */
139 140
    void SetUnit( int aUnit );

141 142 143 144 145 146 147 148 149
    /**
     * change the unit id to aUnit
     * has maening only for multiple parts per package
     * Do not change the modified flag bit, and should be used when
     * change is not due to an edition command
     * @param aUnit = the new unit Id
     */
    void UpdateUnit( int aUnit );

150 151 152 153 154 155
    int GetConvert() const { return m_convert; }

    void SetConvert( int aConvert );

    wxString GetPrefix() const { return m_prefix; }

156 157 158
    TRANSFORM& GetTransform() const { return const_cast< TRANSFORM& >( m_transform ); }

    void SetTransform( const TRANSFORM& aTransform );
charras's avatar
charras committed
159

160 161 162 163 164 165 166 167
    /**
     * Function GetPartCount
     * returns the number of parts per package of the component.
     *
     * @return The number of parts per package or zero if the library entry cannot be found.
     */
    int GetPartCount() const;

168
    bool Save( FILE* aFile ) const;
169

170
    bool Load( LINE_READER& aLine, wxString& aErrorMsg );
171

172 173 174 175 176 177
    /**
     * Function SetOrientation
     * computes the new transform matrix based on \a aOrientation for the component which is
     * applied to the current transform.
     * @param aOrientation The orientation to apply to the transform.
     */
178
    void SetOrientation( int aOrientation );
179

180
    /**
181
     * Function GetOrientation
182 183 184
     * Used to display component orientation (in dialog editor or info)
     * @return the orientation and mirror
     * Note: Because there are different ways to have a given orientation/mirror,
185 186 187
     * the orientation/mirror is not necessary what the used does
     * (example : a mirrorX then a mirrorY give no mirror but rotate the
     * component).
188
     * So this function find a rotation and a mirror value
189
     * ( CMP_MIRROR_X because this is the first mirror option tested)
190
     *  but can differs from the orientation made by an user
191
     * ( a CMP_MIRROR_Y is find as a CMP_MIRROR_X + orientation 180, because
192
     * they are equivalent)
193
     */
194
    int GetOrientation();
195

196 197 198 199 200 201 202
    /**
     * Function GetScreenCoord
     * Returns the coordinated point relative to the orientation of the component of \a aPoint.
     * The coordinates are always relative to the anchor position of the component.
     * @param aPoint The coordinates to transform.
     * @return The transformed point.
     */
203
    wxPoint GetScreenCoord( const wxPoint& aPoint );
204

205
    void GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList );
206

charras's avatar
charras committed
207
    /**
208 209
     * Function ClearAnnotation
     * clears exiting component annotation ( i.i IC23 changed to IC? and part reset to 1)
210 211
     * @param aSheetPath: SCH_SHEET_PATH value: if NULL remove all annotations,
     *                    else remove annotation relative to this sheetpath
charras's avatar
charras committed
212
     */
213
    void ClearAnnotation( SCH_SHEET_PATH* aSheetPath );
charras's avatar
charras committed
214

215 216
    /**
     * Function SetTimeStamp
217 218
     * changes the time stamp to \a aNewTimeStamp updates the reference path.
     * @see m_PathsAndReferences
219 220
     * @param aNewTimeStamp = new time stamp
     */
221
    void SetTimeStamp( time_t aNewTimeStamp );
222

223
    const EDA_RECT GetBoundingBox() const;    // Virtual
224

225 226
    //-----<Fields>-----------------------------------------------------------

227 228 229
    /**
     * Function GetField
     * returns a field.
230
     * @param aFieldNdx An index into the array of fields, not a field id.
231
     * @return SCH_FIELD* - the field value or NULL if does not exist
232
     */
233
    SCH_FIELD* GetField( int aFieldNdx ) const;
234 235

    /**
236
     * Function AddField
dickelbeck's avatar
dickelbeck committed
237 238
     * adds a field to the component.  The field is copied as it is put into
     * the component.
239
     * @param aField A const reference to the SCH_FIELD to add.
240 241 242 243 244 245 246
     * @return SCH_FIELD* - the newly inserted field.
     */
    SCH_FIELD* AddField( const SCH_FIELD& aField );

    /**
     * Function FindField
     * searches for SCH_FIELD with \a aFieldName and returns it if found, else NULL.
247
     */
248
    SCH_FIELD* FindField( const wxString& aFieldName );
249

250
    void SetFields( const SCH_FIELDS& aFields )
251 252 253 254
    {
        m_Fields = aFields;     // vector copying, length is changed possibly
    }

255
    //-----</Fields>----------------------------------------------------------
256

257 258 259 260 261
    /**
     * Function GetFieldCount
     * returns the number of fields in this component.
     */
    int GetFieldCount() const { return (int) m_Fields.size(); }
CHARRAS's avatar
CHARRAS committed
262

263
    /**
264 265
     * Function GetPin
     * finds a component pin by number.
266 267 268 269 270 271
     *
     * @param number - The number of the pin to find.
     * @return Pin object if found, otherwise NULL.
     */
    LIB_PIN* GetPin( const wxString& number );

272 273 274
    void Draw( EDA_DRAW_PANEL* panel,
               wxDC*           DC,
               const wxPoint&  offset,
275
               GR_DRAWMODE     draw_mode,
276
               EDA_COLOR_T     Color = UNSPECIFIED_COLOR )
277 278 279 280
    {
        Draw( panel, DC, offset, draw_mode, Color, true );
    }

281 282 283
    void Draw( EDA_DRAW_PANEL* panel,
               wxDC*           DC,
               const wxPoint&  offset,
284
               GR_DRAWMODE     draw_mode,
285
               EDA_COLOR_T     Color,
286
               bool            DrawPinText );
287

288
    void SwapData( SCH_ITEM* aItem );
289

290
    // returns a unique ID, in the form of a path.
291
    wxString GetPath( const SCH_SHEET_PATH* sheet ) const;
292

293 294 295 296 297 298 299 300
    /**
     * Function IsReferenceStringValid (static)
     * Tests for an acceptable reference string
     * An acceptable reference string must support unannotation
     * i.e starts by letter
     * @param aReferenceString = the reference string to validate
     * @return true if OK
     */
301
    static bool IsReferenceStringValid( const wxString& aReferenceString );
302

303 304 305 306 307
    void SetCurrentSheetPath( const SCH_SHEET_PATH* aSheetPath )
    {
        m_currentSheetPath = aSheetPath;
    }

308 309 310 311
    /**
     * Function GetRef
     * returns the reference, for the given sheet path.
     */
312
    const wxString GetRef( const SCH_SHEET_PATH* sheet );
313

314 315 316
    /**
     * Set the reference, for the given sheet path.
     */
317
    void SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref );
318 319 320

    /**
     * Function AddHierarchicalReference
321
     * adds a full hierarchical reference (path + local reference)
322 323
     * @param aPath Hierarchical path (/&ltsheet timestamp&gt/&ltcomponent
     *              timestamp&gt like /05678E50/A23EF560)
324
     * @param aRef :local reference like C45, R56
325
     * @param aMulti Part selection, used in multi part per package (0 or 1 for non multi)
326
     */
327 328 329
    void AddHierarchicalReference( const wxString& aPath,
                                   const wxString& aRef,
                                   int             aMulti );
330

331
    // returns the unit selection, for the given sheet path.
332
    int GetUnitSelection( SCH_SHEET_PATH* aSheet );
333

334
    // Set the unit selection, for the given sheet path.
335
    void SetUnitSelection( SCH_SHEET_PATH* aSheet, int aUnitSelection );
CHARRAS's avatar
CHARRAS committed
336

337
    // Geometric transforms (used in block operations):
338

339
    void Move( const wxPoint& aMoveVector )
340
    {
341 342 343
        if( aMoveVector == wxPoint( 0, 0 ) )
            return;

344
        m_Pos += aMoveVector;
345

346
        for( int ii = 0; ii < GetFieldCount(); ii++ )
347
            GetField( ii )->Move( aMoveVector );
348

349 350
        SetModified();
    }
351

352
    void MirrorY( int aYaxis_position );
353

354
    void MirrorX( int aXaxis_position );
355

356
    void Rotate( wxPoint aPosition );
357

358
    bool Matches( wxFindReplaceData& aSearchData, void* aAuxData, wxPoint* aFindLocation );
359

360
    void GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList );
361 362 363

    wxPoint GetPinPhysicalPosition( LIB_PIN* Pin );

364
    bool IsSelectStateChanged( const wxRect& aRect );
365

366
    bool IsConnectable() const { return true; }
367

368 369 370 371 372 373 374
    /**
     * @return true if the component is in netlist
     * which means this is not a power component, or something
     * like a component reference starting by #
     */
    bool IsInNetlist() const;

375
    void GetConnectionPoints( std::vector<wxPoint>& aPoints ) const;
376

377
    SEARCH_RESULT Visit( INSPECTOR* inspector, const void* testData,
378 379
                                 const KICAD_T scanTypes[] );

380 381 382 383 384 385 386 387
    /**
     * Function GetDrawItem().
     * Return the component library item at \a aPosition that is part of this component.
     *
     * @param aPosition - Schematic position of the component library object.
     * @param aType - Type of component library object to find or any if set to TYPE_NOT_INIT.
     * @return A pointer to the component library object if found, otherwise NULL.
     */
388
    LIB_ITEM* GetDrawItem( const wxPoint& aPosition, KICAD_T aType = TYPE_NOT_INIT );
389

390
    wxString GetSelectMenuText() const;
391

392
    BITMAP_DEF GetMenuImage() const { return  add_component_xpm; }
393

394 395
    void GetNetListItem( NETLIST_OBJECT_LIST& aNetListItems,
                         SCH_SHEET_PATH*      aSheetPath );
396

397
    bool operator <( const SCH_ITEM& aItem ) const;
398

399 400 401
    bool operator==( const SCH_COMPONENT& aComponent) const;
    bool operator!=( const SCH_COMPONENT& aComponent) const;

402 403
    SCH_ITEM& operator=( const SCH_ITEM& aItem );

404
    bool IsReplaceable() const { return true; }
405

406
    wxPoint GetPosition() const { return m_Pos; }
407

408
    void SetPosition( const wxPoint& aPosition ) { Move( aPosition - m_Pos ); }
409

410
    bool HitTest( const wxPoint& aPosition, int aAccuracy ) const;
411

412
    bool HitTest( const EDA_RECT& aRect, bool aContained = false, int aAccuracy = 0 ) const;
413

414
    void Plot( PLOTTER* aPlotter );
415

416
    EDA_ITEM* Clone() const;
417

418
#if defined(DEBUG)
419
    void Show( int nestLevel, std::ostream& os ) const;     // override
CHARRAS's avatar
CHARRAS committed
420
#endif
421 422

private:
423
    bool doIsConnected( const wxPoint& aPosition ) const;
plyatov's avatar
plyatov committed
424 425 426 427
};


#endif /* COMPONENT_CLASS_H */