code easier to understand

This commit is contained in:
charras 2009-10-06 13:52:43 +00:00
parent c974c42daf
commit 6c3968a178
3 changed files with 71 additions and 46 deletions

View File

@ -1168,6 +1168,18 @@ void LIB_COMPONENT::MirrorSelectedItemsH( const wxPoint& center )
} }
/**
* Locate a draw object.
*
* @param unit - Unit number of draw item.
* @param convert - Body style of draw item.
* @param type - Draw object type, set to 0 to search for any type.
* @param pt - Coordinate for hit testing.
*
* @return LIB_DRAW_ITEM - Pointer the the draw object if found.
* Otherwise NULL.
*/
LIB_DRAW_ITEM* LIB_COMPONENT::LocateDrawItem( int unit, int convert, LIB_DRAW_ITEM* LIB_COMPONENT::LocateDrawItem( int unit, int convert,
KICAD_T type, const wxPoint& pt ) KICAD_T type, const wxPoint& pt )
{ {
@ -1201,6 +1213,46 @@ LIB_DRAW_ITEM* LIB_COMPONENT::LocateDrawItem( int unit, int convert,
return NULL; return NULL;
} }
/** Function HitTest (overlaid)
* @return true if the point aPosRef is near this object
* @param aPosRef = a wxPoint to test
* @param aThreshold = max distance to this object (usually the half
* thickness of a line)
* @param aTransMat = the transform matrix
*
* @return LIB_DRAW_ITEM - Pointer the the draw object if found.
* Otherwise NULL.
*/
LIB_DRAW_ITEM* LIB_COMPONENT::LocateDrawItem( int unit, int convert,
KICAD_T type, const wxPoint& pt, const int aTransMat[2][2] )
{
/* we use LocateDrawItem( int unit, int convert, KICAD_T type, const wxPoint& pt )
* to search items.
* because this function uses DefaultTransformMatrix as orient/mirror matrix
* we temporary copy aTransMat in DefaultTransformMatrix
*/
LIB_DRAW_ITEM * item;
int matrix[2][2];
for ( int ii =0; ii<2;ii++ )
{
for ( int jj =0; jj<2;jj++ )
{
matrix[ii][jj] = aTransMat[ii][jj];
EXCHG(matrix[ii][jj], DefaultTransformMatrix[ii][jj]);
}
}
item = LocateDrawItem( unit, convert, type, pt );
//Restore matrix
for ( int ii =0; ii<2;ii++ )
{
for ( int jj =0; jj<2;jj++ )
{
EXCHG(matrix[ii][jj], DefaultTransformMatrix[ii][jj]);
}
}
return item;
}
void LIB_COMPONENT::SetPartCount( int count ) void LIB_COMPONENT::SetPartCount( int count )
{ {

View File

@ -356,6 +356,21 @@ public:
LIB_DRAW_ITEM* LocateDrawItem( int unit, int convert, KICAD_T type, LIB_DRAW_ITEM* LocateDrawItem( int unit, int convert, KICAD_T type,
const wxPoint& pt ); const wxPoint& pt );
/**
* Locate a draw object (overlaid)
*
* @param unit - Unit number of draw item.
* @param convert - Body style of draw item.
* @param type - Draw object type, set to 0 to search for any type.
* @param pt - Coordinate for hit testing.
* @param aTransMat = the transform matrix
*
* @return LIB_DRAW_ITEM - Pointer the the draw object if found.
* Otherwise NULL.
*/
LIB_DRAW_ITEM* LocateDrawItem( int unit, int convert, KICAD_T type,
const wxPoint& pt, const int aTransMat[2][2] );
/** /**
* Return a reference to the draw item list. * Return a reference to the draw item list.
* *

View File

@ -686,38 +686,6 @@ Hierarchical_PIN_Sheet_Struct* LocateSheetLabel( DrawSheetStruct* Sheet,
return NULL; return NULL;
} }
/* helper function used to locate graphics items in a lib component (in library space)
* to a given location given in schematic space
* in schematic space, a component is an image of the lib component, rotated and mirorred
* by its mirror/rotation matrix
* this function calculates the invert matrix of the mirror/rotation matrix
* it is used to calculate the position in in library space from
* the position in schematic space of a test point, corresponding to a given component
*/
bool InvertMatrix(int aSource[2][2], int aDest[2][2] )
{
/* for a source matrix (a,b, c,d) a, if the first line, and cd the second line
* the invert matrix is 1/det * comatrix
* det = ad-bc
* comatrix = (d,-b, -c,a)
* a = aSource[0][0]
* b = aSource[0][1]
* c = aSource[1][0]
* d = aSource[1][1]
* in eeschema, values are 1, 0 or -1 only and we can use integers only
*/
bool success = true;
int det = aSource[0][0]*aSource[1][1] - aSource[0][1]*aSource[1][0];
wxASSERT(det);
if( det == 0 ) // Should not occur with eeschema matrix transform
det = 1;
aDest[0][0] = aSource[1][1]/det;
aDest[0][1] = -aSource[0][1]/det;
aDest[1][0] = -aSource[1][0]/det;
aDest[1][1] = aSource[0][0]/det;
return success;
}
LibDrawPin* LocateAnyPin( SCH_ITEM* DrawList, const wxPoint& RefPos, LibDrawPin* LocateAnyPin( SCH_ITEM* DrawList, const wxPoint& RefPos,
SCH_COMPONENT** libpart ) SCH_COMPONENT** libpart )
@ -738,24 +706,14 @@ LibDrawPin* LocateAnyPin( SCH_ITEM* DrawList, const wxPoint& RefPos,
if( Entry == NULL ) if( Entry == NULL )
continue; continue;
/* we use LocateDrawItem to locate pîns. but this function suppose a component /* we use LocateDrawItem to locate pîns. but this function suppose a component
* at 0,0 location, in normal orientation/mirror * at 0,0 location
* So we must calculate the ref position in component space * So we must calculate the ref position relative to the component
*/ */
// Calculate the position relative to the component (in library space the component is at location 0,0)
wxPoint libPos = RefPos - schItem->m_Pos; wxPoint libPos = RefPos - schItem->m_Pos;
// Calculate the equivalent position of the test point for a normal orient component
int itransMat[2][2];
InvertMatrix(schItem->m_Transform, itransMat );
libPos = TransformCoordinate(itransMat, libPos);
// LocateDrawItem uses DefaultTransformMatrix as matrix orientation of the component
// so we must recalculate libPos for this orientation before calling LocateDrawItem
InvertMatrix(DefaultTransformMatrix, itransMat );
libPos = TransformCoordinate(itransMat, libPos);
wxPoint schPos = TransformCoordinate(schItem->m_Transform, libPos);
Pin = (LibDrawPin*) Entry->LocateDrawItem( schItem->m_Multi, Pin = (LibDrawPin*) Entry->LocateDrawItem( schItem->m_Multi,
schItem->m_Convert, schItem->m_Convert,
COMPONENT_PIN_DRAW_TYPE, COMPONENT_PIN_DRAW_TYPE,
libPos ); libPos, schItem->m_Transform );
if( Pin ) if( Pin )
break; break;
} }