KiCad PCB EDA Suite
dimension.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
10  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
11  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, you may find one here:
25  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
26  * or you may search the http://www.gnu.org website for the version 2 license,
27  * or you may write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30 
31 #include <fctsys.h>
32 #include <confirm.h>
33 #include <gr_basic.h>
34 #include <class_drawpanel.h>
35 #include <pcb_edit_frame.h>
36 #include <draw_graphic_text.h>
37 #include <dialog_helpers.h>
38 #include <macros.h>
39 #include <base_units.h>
40 #include <board_commit.h>
41 
42 #include <class_board.h>
43 #include <class_pcb_text.h>
44 #include <class_dimension.h>
45 
46 #include <pcbnew.h>
47 #include <pcb_layer_box_selector.h>
49 
50 /* Local functions */
51 static void BuildDimension( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
52  const wxPoint& aPosition, bool aErase );
53 
54 static void MoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
55  const wxPoint& aPosition, bool aErase );
56 static void AbortMoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC );
57 
58 
59 /* Local variables : */
60 static int status_dimension; /* Used in dimension creation:
61  * = 0 : initial value: no dimension in progress
62  * = 1 : First point created
63  * = 2 : Second point created, the text must be placed */
64 
65 /*
66  * A dimension has this shape:
67  * It has 2 reference points, and a text
68  * | |
69  * | dist |
70  * |<---------->|
71  * | |
72  *
73  */
74 
75 static void AbortBuildDimension( EDA_DRAW_PANEL* Panel, wxDC* aDC )
76 {
77  DIMENSION* dimension = (DIMENSION*) Panel->GetScreen()->GetCurItem();
78 
79  if( dimension )
80  {
81  if( dimension->IsNew() )
82  {
83  dimension->Draw( Panel, aDC, GR_XOR );
84  dimension->DeleteStructure();
85  }
86  else
87  {
88  dimension->Draw( Panel, aDC, GR_OR );
89  }
90  }
91 
92  status_dimension = 0;
93  ((PCB_EDIT_FRAME*)Panel->GetParent())->SetCurItem( NULL );
94 }
95 
96 
98 {
99  wxPoint pos;
100 
101  if( aDimension == NULL )
102  {
103  const BOARD_DESIGN_SETTINGS& boardSettings = GetBoard()->GetDesignSettings();
104 
105  status_dimension = 1;
106  pos = GetCrossHairPosition();
107 
108  aDimension = new DIMENSION( GetBoard() );
109  aDimension->SetFlags( IS_NEW );
110  aDimension->SetLayer( GetActiveLayer() );
111  aDimension->SetOrigin( pos );
112  aDimension->SetEnd( pos );
113 
114  aDimension->Text().SetTextSize( boardSettings.GetTextSize( GetActiveLayer() ) );
115  aDimension->Text().SetThickness( boardSettings.GetTextThickness( GetActiveLayer() ) );
116  aDimension->Text().SetItalic( boardSettings.GetTextItalic( GetActiveLayer() ) );
117  aDimension->SetWidth( boardSettings.GetLineThickness( GetActiveLayer() ) );
118  aDimension->AdjustDimensionDetails();
119  aDimension->Draw( m_canvas, aDC, GR_XOR );
120 
122  return aDimension;
123  }
124 
125  // Dimension != NULL
126  if( status_dimension == 1 )
127  {
128  status_dimension = 2;
129  return aDimension;
130  }
131 
132  aDimension->Draw( m_canvas, aDC, GR_OR );
133  aDimension->ClearFlags();
134 
135  /* ADD this new item in list */
136  GetBoard()->Add( aDimension );
137 
138  // Add store it in undo/redo list
139  SaveCopyInUndoList( aDimension, UR_NEW );
140 
141  OnModify();
142  m_canvas->SetMouseCapture( NULL, NULL );
143 
144  return NULL;
145 }
146 
147 
148 static void BuildDimension( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
149  const wxPoint& aPosition, bool aErase )
150 {
151  PCB_SCREEN* screen = (PCB_SCREEN*) aPanel->GetScreen();
152  DIMENSION* Dimension = (DIMENSION*) screen->GetCurItem();
153  wxPoint pos = aPanel->GetParent()->GetCrossHairPosition();
154 
155  if( Dimension == NULL )
156  return;
157 
158  // Erase previous dimension.
159  if( aErase )
160  {
161  Dimension->Draw( aPanel, aDC, GR_XOR );
162  }
163 
164  Dimension->SetLayer( screen->m_Active_Layer );
165 
166  if( status_dimension == 1 )
167  {
168  Dimension->m_featureLineDO = pos;
169  Dimension->m_crossBarF = Dimension->m_featureLineDO;
170  Dimension->AdjustDimensionDetails();
171  }
172  else
173  {
174  /* Calculating the direction of travel perpendicular to the selected axis. */
175  double angle = Dimension->GetAngle() + (M_PI / 2);
176 
177  wxPoint delta = pos - Dimension->m_featureLineDO;
178  double depl = ( delta.x * cos( angle ) ) + ( delta.y * sin( angle ) );
179  Dimension->SetHeight( depl );
180  }
181 
182  Dimension->Draw( aPanel, aDC, GR_XOR );
183 }
184 
185 
187 {
188  if( aDimension == NULL )
189  return;
190 
191  DIALOG_TEXT_PROPERTIES dlg( this, aDimension, aDC );
192  dlg.ShowModal();
193 }
194 
195 
196 void PCB_EDIT_FRAME::DeleteDimension( DIMENSION* aDimension, wxDC* aDC )
197 {
198  if( aDimension == NULL )
199  return;
200 
201  if( aDC )
202  aDimension->Draw( m_canvas, aDC, GR_XOR );
203 
204  SaveCopyInUndoList( aDimension, UR_DELETED );
205  aDimension->UnLink();
206  OnModify();
207 }
208 
209 
210 /* Initialize parameters to move a pcb text
211  */
213 
215 {
216  if( aItem == NULL )
217  return;
218 
219  // Store the initial position for undo/abort command
220  initialTextPosition = aItem->Text().GetTextPos();
221 
222  aItem->Draw( m_canvas, DC, GR_XOR );
223  aItem->SetFlags( IS_MOVED );
224  SetMsgPanel( aItem );
225 
226  SetCrossHairPosition( aItem->Text().GetTextPos() );
228 
230  SetCurItem( aItem );
231  m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
232 }
233 
234 
235 /* Move dimension text following the cursor. */
236 static void MoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
237  bool aErase )
238 {
239  DIMENSION* dimension = (DIMENSION*) aPanel->GetScreen()->GetCurItem();
240 
241  if( dimension == NULL )
242  return;
243 
244  if( aErase )
245  dimension->Draw( aPanel, aDC, GR_XOR );
246 
247  dimension->Text().SetTextPos( aPanel->GetParent()->GetCrossHairPosition() );
248 
249  dimension->Draw( aPanel, aDC, GR_XOR );
250 }
251 
252 
253 /*
254  * Abort current text edit progress.
255  *
256  * If a text is selected, its initial coord are regenerated
257  */
258 void AbortMoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC )
259 {
260  DIMENSION* dimension = (DIMENSION*) aPanel->GetScreen()->GetCurItem();
261  ( (PCB_EDIT_FRAME*) aPanel->GetParent() )->SetCurItem( NULL );
262 
263  aPanel->SetMouseCapture( NULL, NULL );
264 
265  if( dimension == NULL ) // Should not occur
266  return;
267 
268  dimension->Draw( aPanel, aDC, GR_XOR );
269  dimension->Text().SetTextPos( initialTextPosition );
270  dimension->ClearFlags();
271  dimension->Draw( aPanel, aDC, GR_OR );
272 }
273 
274 
275 /*
276  * Place the current dimension text being moving
277  */
279 {
280  m_canvas->SetMouseCapture( NULL, NULL );
281  SetCurItem( NULL );
282 
283  if( aItem == NULL )
284  return;
285 
286  aItem->Draw( m_canvas, DC, GR_OR );
287  OnModify();
288 
289  wxPoint tmp = aItem->Text().GetTextPos();
290  aItem->Text().SetTextPos( initialTextPosition );
291  SaveCopyInUndoList( aItem, UR_CHANGED );
292  aItem->Text().SetTextPos( tmp );
293  aItem->ClearFlags();
294 }
virtual BASE_SCREEN * GetScreen()=0
virtual void OnModify() override
Function OnModify must be called after a board change to set the modified flag.
void DeleteDimension(DIMENSION *aDimension, wxDC *aDC)
Definition: dimension.cpp:196
TEXTE_PCB class definition.
bool GetTextItalic(PCB_LAYER_ID aLayer) const
const wxPoint & GetTextPos() const
Definition: eda_text.h:237
EDA_ITEM * GetCurItem() const
Definition: base_screen.h:233
Implementation of conversion functions that require both schematic and board internal units...
This file is part of the common library.
wxPoint m_crossBarF
bool IsNew() const
Definition: base_struct.h:219
void PlaceDimensionText(DIMENSION *aItem, wxDC *DC)
Definition: dimension.cpp:278
Class BOARD to handle a board.
void SetItalic(bool isItalic)
Definition: eda_text.h:182
void BeginMoveDimensionText(DIMENSION *aItem, wxDC *DC)
Definition: dimension.cpp:214
void ShowDimensionPropertyDialog(DIMENSION *aDimension, wxDC *aDC)
Definition: dimension.cpp:186
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:236
void SetCurItem(BOARD_ITEM *aItem, bool aDisplayInfo=true)
Function SetCurItem sets the currently selected item and displays it in the MsgPanel.
void Draw(EDA_DRAW_PANEL *panel, wxDC *DC, GR_DRAWMODE aColorMode, const wxPoint &offset=ZeroOffset) override
Function Draw BOARD_ITEMs have their own color information.
void DeleteStructure()
Function DeleteStructure deletes this object after UnLink()ing it from its owner if it has one...
BOARD * GetBoard() const
int GetTextThickness(PCB_LAYER_ID aLayer) const
Function GetTextThickness Returns the default text thickness from the layer class for the given layer...
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:227
DIMENSION * EditDimension(DIMENSION *aDimension, wxDC *aDC)
Definition: dimension.cpp:97
virtual void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
virtual void UnLink()
Function UnLink detaches this object from its owner.
double GetAngle() const
Function GetAngle Returns angle of the crossbar.
PCB_LAYER_ID m_Active_Layer
Definition: pcb_screen.h:44
wxSize GetTextSize(PCB_LAYER_ID aLayer) const
Function GetTextSize Returns the default text size from the layer class for the given layer...
static const int delta[8][2]
Definition: solve.cpp:112
void SetLayer(PCB_LAYER_ID aLayer) override
Function SetLayer sets the layer this item is on.
This file contains miscellaneous commonly used macros and functions.
static void AbortBuildDimension(EDA_DRAW_PANEL *Panel, wxDC *aDC)
Definition: dimension.cpp:75
#define IS_NEW
New item, just created.
Definition: base_struct.h:114
virtual EDA_DRAW_FRAME * GetParent() const =0
DIMENSION class definition.
static void MoveDimensionText(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Definition: dimension.cpp:236
void SetOrigin(const wxPoint &aOrigin)
Function SetOrigin Sets a new origin of the crossbar line.
BOARD_ITEM * GetCurItem() const
Function GetCurItem returns the currently selected BOARD_ITEM, overriding BASE_SCREEN::GetCurItem().
Definition: pcb_screen.h:72
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:256
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
virtual void SetMouseCapture(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback, END_MOUSE_CAPTURE_CALLBACK aEndMouseCaptureCallback)
Function SetMouseCapture sets the mouse capture and end mouse capture callbacks to aMouseCaptureCallb...
void SetEnd(const wxPoint &aEnd)
Function SetEnd Sets a new end of the crossbar line.
static int status_dimension
Definition: dimension.cpp:60
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
virtual void CallMouseCapture(wxDC *aDC, const wxPoint &aPosition, bool aErase)
Function CallMouseCapture calls the mouse capture callback.
Helper dialog and control classes.
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:538
void AdjustDimensionDetails()
Function AdjustDimensionDetails Calculate coordinates of segments used to draw the dimension...
static void BuildDimension(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Definition: dimension.cpp:148
void SaveCopyInUndoList(BOARD_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0)) override
Function SaveCopyInUndoList Creates a new entry in undo list of commands.
Definition: undo_redo.cpp:202
int GetLineThickness(PCB_LAYER_ID aLayer) const
Function GetLineThickness Returns the default graphic segment thickness from the layer class for the ...
static wxPoint initialTextPosition
Definition: dimension.cpp:212
Definition: gr_basic.h:38
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:123
void SetHeight(int aHeight)
Function SetHeight Sets the length of feature lines.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
TEXTE_PCB & Text()
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:257
static void AbortMoveDimensionText(EDA_DRAW_PANEL *aPanel, wxDC *aDC)
Definition: dimension.cpp:258
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Set the screen cross hair position to aPosition in logical (drawing) units.
void SetWidth(int aWidth)
wxPoint m_featureLineDO
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Return the current cross hair position in logical (drawing) coordinates.
virtual PCB_LAYER_ID GetActiveLayer() const
Function GetActiveLayer returns the active layer.
Class DIMENSION.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:161
Class BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.
#define IS_MOVED
Item being moved.
Definition: base_struct.h:113