CADSTAR PCB Archive Importer: Load Board shapes

This commit is contained in:
Roberto Fernandez Bautista 2020-08-16 18:12:08 +01:00 committed by Seth Hillbrand
parent a7effb7377
commit a9ef38e6c8
5 changed files with 341 additions and 49 deletions

View File

@ -248,6 +248,20 @@ template <class T> inline T NormalizeAngle360Min( T Angle )
return Angle;
}
/// Normalize angle to be in the 0.0 .. -360.0 range:
/// angle is in 1/10 degrees
template <class T>
inline T NormalizeAngleNeg( T Angle )
{
while( Angle < -3600 )
Angle += 3600;
while( Angle >= 0 )
Angle -= 3600;
return Angle;
}
/// Normalize angle to be in the 0.0 .. 360.0 range:
/// angle is in 1/10 degrees
template <class T> inline T NormalizeAnglePos( T Angle )

View File

@ -42,8 +42,8 @@ void CADSTAR_ARCHIVE_COMMON::POINT::Parse( XNODE* aNode )
{
wxASSERT( aNode->GetName() == wxT( "PT" ) );
X = GetXmlAttributeIDLong( aNode, 0 );
Y = GetXmlAttributeIDLong( aNode, 1 );
x = GetXmlAttributeIDLong( aNode, 0 );
y = GetXmlAttributeIDLong( aNode, 1 );
}
@ -68,8 +68,8 @@ void CADSTAR_ARCHIVE_COMMON::VERTEX::Parse( XNODE* aNode )
if( aNodeName == wxT( "PT" ) )
{
Type = VERTEX_TYPE::POINT;
Center.X = UNDEFINED_VALUE;
Center.Y = UNDEFINED_VALUE;
Center.x = UNDEFINED_VALUE;
Center.y = UNDEFINED_VALUE;
End.Parse( aNode );
}
else if( aNodeName == wxT( "ACWARC" ) || aNodeName == wxT( "CWARC" ) )
@ -91,8 +91,8 @@ void CADSTAR_ARCHIVE_COMMON::VERTEX::Parse( XNODE* aNode )
else
Type = VERTEX_TYPE::CLOCKWISE_SEMICIRCLE;
Center.X = UNDEFINED_VALUE;
Center.Y = UNDEFINED_VALUE;
Center.x = UNDEFINED_VALUE;
Center.y = UNDEFINED_VALUE;
std::vector<POINT> pts = ParseAllChildPoints( aNode, true, 1 );

View File

@ -74,10 +74,11 @@ public:
/**
* @brief Represents a point in x,y coordinates
*/
struct POINT
struct POINT : wxPoint
{
long X = UNDEFINED_VALUE;
long Y = UNDEFINED_VALUE;
POINT() : wxPoint( UNDEFINED_VALUE, UNDEFINED_VALUE )
{
}
void Parse( XNODE* aNode );
};

View File

@ -23,16 +23,43 @@
* @brief Converts a CADSTAR_PCB_ARCHIVE_PARSER object into a KiCad BOARD object
*/
#include <board_stackup_manager/stackup_predefined_prms.h> //KEY_COPPER, KEY_CORE, KEY_PREPREG
#include <cadstar_pcb.h>
#include <board_stackup_manager/stackup_predefined_prms.h> //KEY_COPPER, KEY_CORE, KEY_PREPREG
#include <class_drawsegment.h> // DRAWSEGMENT
#include <limits> // std::numeric_limits
#include <trigo.h>
void CADSTAR_PCB::Load( ::BOARD* aBoard )
{
mBoard = aBoard;
Parse();
loadBoardStackup();
wxPoint designSize =
Assignments.Technology.DesignArea.first - Assignments.Technology.DesignArea.second;
long long designSizeXkicad = (long long) designSize.x * KiCadUnitMultiplier;
long long designSizeYkicad = (long long) designSize.y * KiCadUnitMultiplier;
long long maxDesignSizekicad = (long long) std::numeric_limits<int>::max()
+ std::abs( std::numeric_limits<int>::min() );
if( designSizeXkicad > maxDesignSizekicad || designSizeYkicad > maxDesignSizekicad )
THROW_IO_ERROR( wxString::Format(
_( "The design is too large and cannot be imported into KiCad. \n"
"Please reduce the maximum design size in CADSTAR by navigating to: \n"
"Design Tab -> Properties -> Design Options -> Maximum Design Size. \n"
"Current Design size: %d, %d micrometers. \n"
"Maximum permitted design size: %d, %d micrometers.\n" ),
designSizeXkicad / 1000, designSizeYkicad / 1000, maxDesignSizekicad / 1000,
maxDesignSizekicad / 1000 ) );
mDesignCenter =
( Assignments.Technology.DesignArea.first + Assignments.Technology.DesignArea.second )
/ 2;
loadBoardStackup();
loadBoards();
//TODO: process all other items
}
@ -93,19 +120,19 @@ void CADSTAR_PCB::loadBoardStackup()
continue;
case LAYER_TYPE::JUMPERLAYER:
copperType = LAYER_T::LT_JUMPER;
kicadLayerID = getKiCadCopperLayerID( numElecAndPowerLayers++ );
kicadLayerID = getKiCadCopperLayerID( ++numElecAndPowerLayers );
kicadLayerType = BOARD_STACKUP_ITEM_TYPE::BS_ITEM_TYPE_COPPER;
layerTypeName = KEY_COPPER;
break;
case LAYER_TYPE::ELEC:
copperType = LAYER_T::LT_SIGNAL;
kicadLayerID = getKiCadCopperLayerID( numElecAndPowerLayers++ );
kicadLayerID = getKiCadCopperLayerID( ++numElecAndPowerLayers );
kicadLayerType = BOARD_STACKUP_ITEM_TYPE::BS_ITEM_TYPE_COPPER;
layerTypeName = KEY_COPPER;
break;
case LAYER_TYPE::POWER:
copperType = LAYER_T::LT_POWER;
kicadLayerID = getKiCadCopperLayerID( numElecAndPowerLayers++ );
kicadLayerID = getKiCadCopperLayerID( ++numElecAndPowerLayers );
kicadLayerType = BOARD_STACKUP_ITEM_TYPE::BS_ITEM_TYPE_COPPER;
layerTypeName = KEY_COPPER;
break;
@ -256,74 +283,239 @@ void CADSTAR_PCB::loadBoardStackup()
}
void CADSTAR_PCB::loadBoards()
{
for( std::pair<BOARD_ID, BOARD> boardPair : Layout.Boards )
{
BOARD& board = boardPair.second;
drawCadstarShape( board.Shape, PCB_LAYER_ID::Edge_Cuts, board.LineCodeID );
//TODO process board attributes
//TODO process addition to a group
}
}
void CADSTAR_PCB::drawCadstarShape( const SHAPE& aCadstarShape, const PCB_LAYER_ID& aKiCadLayer,
const LINECODE_ID& aCadstarLinecodeID )
{
int lineThickness = getLineThickness( aCadstarLinecodeID );
switch( aCadstarShape.Type )
{
case SHAPE_TYPE::OPENSHAPE:
case SHAPE_TYPE::OUTLINE:
drawCadstarVerticesAsSegments( aCadstarShape.Vertices, aKiCadLayer, lineThickness );
drawCadstarCutoutsAsSegments( aCadstarShape.Cutouts, aKiCadLayer, lineThickness );
break;
case SHAPE_TYPE::SOLID:
//TODO
break;
case SHAPE_TYPE::HATCHED:
//TODO
break;
}
}
void CADSTAR_PCB::drawCadstarCutoutsAsSegments( const std::vector<CUTOUT>& aCutouts,
const PCB_LAYER_ID& aKiCadLayer, const int& aLineThickness )
{
for( CUTOUT cutout : aCutouts )
{
drawCadstarVerticesAsSegments( cutout.Vertices, aKiCadLayer, aLineThickness );
}
}
void CADSTAR_PCB::drawCadstarVerticesAsSegments( const std::vector<VERTEX>& aCadstarVertices,
const PCB_LAYER_ID& aKiCadLayer, const int& aLineThickness )
{
std::vector<DRAWSEGMENT*> drawSegments = getDrawSegments( aCadstarVertices );
for( DRAWSEGMENT* ds : drawSegments )
{
ds->SetWidth( aLineThickness );
ds->SetLayer( aKiCadLayer );
mBoard->Add( ds, ADD_MODE::APPEND );
}
}
std::vector<DRAWSEGMENT*> CADSTAR_PCB::getDrawSegments(
const std::vector<VERTEX>& aCadstarVertices )
{
std::vector<DRAWSEGMENT*> drawSegments;
if( aCadstarVertices.size() < 2 )
//need at least two points to draw a segment! (unlikely but possible to have only one)
return drawSegments;
const VERTEX* prev = &aCadstarVertices.at( 0 ); // first one should always be a point vertex
double arcStartAngle, arcEndAngle, arcAngle;
bool cw = false;
for( size_t i = 1; i < aCadstarVertices.size(); i++ )
{
const VERTEX* cur = &aCadstarVertices[i];
DRAWSEGMENT* ds = new DRAWSEGMENT( mBoard );
cw = false;
wxPoint startPoint = getKiCadPoint( prev->End );
wxPoint endPoint = getKiCadPoint( cur->End );
wxPoint centerPoint;
if( cur->Type == VERTEX_TYPE::ANTICLOCKWISE_SEMICIRCLE
|| cur->Type == VERTEX_TYPE::CLOCKWISE_SEMICIRCLE )
centerPoint = ( startPoint + endPoint ) / 2;
else
centerPoint = getKiCadPoint( cur->Center );
switch( cur->Type )
{
case VERTEX_TYPE::POINT:
ds->SetShape( STROKE_T::S_SEGMENT );
ds->SetStart( startPoint );
ds->SetEnd( endPoint );
break;
case VERTEX_TYPE::CLOCKWISE_SEMICIRCLE:
case VERTEX_TYPE::CLOCKWISE_ARC:
cw = true;
case VERTEX_TYPE::ANTICLOCKWISE_SEMICIRCLE:
case VERTEX_TYPE::ANTICLOCKWISE_ARC:
ds->SetShape( STROKE_T::S_ARC );
ds->SetArcStart( startPoint );
ds->SetCenter( centerPoint );
arcStartAngle = getPolarAngle( startPoint - centerPoint );
arcEndAngle = getPolarAngle( endPoint - centerPoint );
arcAngle = arcEndAngle - arcStartAngle;
//TODO: detect if we are supposed to draw a circle instead (i.e. when two SEMICIRCLE with oppositve state/end points)
if( cw )
ds->SetAngle( NormalizeAnglePos( arcAngle ) );
else
ds->SetAngle( NormalizeAngleNeg( arcAngle ) );
break;
}
drawSegments.push_back( ds );
prev = cur;
}
return drawSegments;
}
int CADSTAR_PCB::getLineThickness( const LINECODE_ID& aCadstarLineCodeID )
{
if( Assignments.Codedefs.LineCodes.find( aCadstarLineCodeID )
== Assignments.Codedefs.LineCodes.end() )
return mBoard->GetDesignSettings().GetLineThickness( PCB_LAYER_ID::Edge_Cuts );
else
return Assignments.Codedefs.LineCodes[aCadstarLineCodeID].Width * KiCadUnitMultiplier;
}
wxPoint CADSTAR_PCB::getKiCadPoint( wxPoint aCadstarPoint )
{
wxPoint retval;
retval.x = ( aCadstarPoint.x - mDesignCenter.x ) * KiCadUnitMultiplier;
retval.y = -( aCadstarPoint.y - mDesignCenter.y ) * KiCadUnitMultiplier;
return retval;
}
double CADSTAR_PCB::getPolarAngle( wxPoint aPoint )
{
return NormalizeAnglePos( ArcTangente( aPoint.y, aPoint.x ) );
}
PCB_LAYER_ID CADSTAR_PCB::getKiCadCopperLayerID( unsigned int aLayerNum )
{
switch( aLayerNum )
{
case 0:
return PCB_LAYER_ID::F_Cu;
case 1:
return PCB_LAYER_ID::In1_Cu;
return PCB_LAYER_ID::F_Cu;
case 2:
return PCB_LAYER_ID::In2_Cu;
return PCB_LAYER_ID::In1_Cu;
case 3:
return PCB_LAYER_ID::In3_Cu;
return PCB_LAYER_ID::In2_Cu;
case 4:
return PCB_LAYER_ID::In4_Cu;
return PCB_LAYER_ID::In3_Cu;
case 5:
return PCB_LAYER_ID::In5_Cu;
return PCB_LAYER_ID::In4_Cu;
case 6:
return PCB_LAYER_ID::In6_Cu;
return PCB_LAYER_ID::In5_Cu;
case 7:
return PCB_LAYER_ID::In7_Cu;
return PCB_LAYER_ID::In6_Cu;
case 8:
return PCB_LAYER_ID::In8_Cu;
return PCB_LAYER_ID::In7_Cu;
case 9:
return PCB_LAYER_ID::In9_Cu;
return PCB_LAYER_ID::In8_Cu;
case 10:
return PCB_LAYER_ID::In10_Cu;
return PCB_LAYER_ID::In9_Cu;
case 11:
return PCB_LAYER_ID::In11_Cu;
return PCB_LAYER_ID::In10_Cu;
case 12:
return PCB_LAYER_ID::In12_Cu;
return PCB_LAYER_ID::In11_Cu;
case 13:
return PCB_LAYER_ID::In13_Cu;
return PCB_LAYER_ID::In12_Cu;
case 14:
return PCB_LAYER_ID::In14_Cu;
return PCB_LAYER_ID::In13_Cu;
case 15:
return PCB_LAYER_ID::In15_Cu;
return PCB_LAYER_ID::In14_Cu;
case 16:
return PCB_LAYER_ID::In16_Cu;
return PCB_LAYER_ID::In15_Cu;
case 17:
return PCB_LAYER_ID::In17_Cu;
return PCB_LAYER_ID::In16_Cu;
case 18:
return PCB_LAYER_ID::In18_Cu;
return PCB_LAYER_ID::In17_Cu;
case 19:
return PCB_LAYER_ID::In19_Cu;
return PCB_LAYER_ID::In18_Cu;
case 20:
return PCB_LAYER_ID::In20_Cu;
return PCB_LAYER_ID::In19_Cu;
case 21:
return PCB_LAYER_ID::In21_Cu;
return PCB_LAYER_ID::In20_Cu;
case 22:
return PCB_LAYER_ID::In22_Cu;
return PCB_LAYER_ID::In21_Cu;
case 23:
return PCB_LAYER_ID::In23_Cu;
return PCB_LAYER_ID::In22_Cu;
case 24:
return PCB_LAYER_ID::In24_Cu;
return PCB_LAYER_ID::In23_Cu;
case 25:
return PCB_LAYER_ID::In25_Cu;
return PCB_LAYER_ID::In24_Cu;
case 26:
return PCB_LAYER_ID::In26_Cu;
return PCB_LAYER_ID::In25_Cu;
case 27:
return PCB_LAYER_ID::In27_Cu;
return PCB_LAYER_ID::In26_Cu;
case 28:
return PCB_LAYER_ID::In28_Cu;
return PCB_LAYER_ID::In27_Cu;
case 29:
return PCB_LAYER_ID::In29_Cu;
return PCB_LAYER_ID::In28_Cu;
case 30:
return PCB_LAYER_ID::In30_Cu;
return PCB_LAYER_ID::In29_Cu;
case 31:
return PCB_LAYER_ID::In30_Cu;
case 32:
return PCB_LAYER_ID::B_Cu;
}
return PCB_LAYER_ID::UNDEFINED_LAYER;
}
PCB_LAYER_ID CADSTAR_PCB::getKiCadLayer( const LAYER_ID& aCadstarLayerID )
{
if( mLayermap.find( aCadstarLayerID ) == mLayermap.end() )
return PCB_LAYER_ID::Cmts_User; // TODO need to handle ALLELEC, ALLLAYER, ALLDOC,
// etc. For now just put unmapped layers here
else
return mLayermap[aCadstarLayerID];
}

View File

@ -35,8 +35,11 @@ class CADSTAR_PCB : public CADSTAR_PCB_ARCHIVE_PARSER
public:
explicit CADSTAR_PCB( wxString aFilename ) : CADSTAR_PCB_ARCHIVE_PARSER( aFilename )
{
mDesignCenter.x = 0;
mDesignCenter.y = 0;
}
/**
* @brief Loads a CADSTAR PCB Archive file into the KiCad BOARD object given
* @param aBoard
@ -45,10 +48,92 @@ public:
private:
::BOARD* mBoard;
std::map<LAYER_ID, PCB_LAYER_ID> mLayermap; //<Map between Cadstar and KiCad Layers
std::map<LAYER_ID, PCB_LAYER_ID> mLayermap; ///<Map between Cadstar and KiCad Layers TODO: convert to a map of LSET to allow multiple layer mappings (e.g. ALLDOC, ALLELEC)
std::map<PHYSICAL_LAYER_ID, LAYER_ID> mCopperLayers;
wxPoint mDesignCenter; ///< Used for calculating the required offset to apply to the Cadstar design so that it fits in KiCad canvas
// Functions for loading individual elements:
void loadBoardStackup();
void loadBoards();
/**
* @brief
* @param aCadstarShape
* @param aCadstarLayerID KiCad layer to draw on
* @param aCadstarLinecodeID Thickness of line to draw with
*/
void drawCadstarShape( const SHAPE& aCadstarShape, const PCB_LAYER_ID& aKiCadLayer,
const LINECODE_ID& aCadstarLinecodeID );
/**
* @brief Uses DRAWSEGMENT to draw the cutouts on mBoard object
* @param aVertices
* @param aKiCadLayer KiCad layer to draw on
* @param aLineThickness Thickness of line to draw with
*/
void drawCadstarCutoutsAsSegments( const std::vector<CUTOUT>& aCutouts,
const PCB_LAYER_ID& aKiCadLayer, const int& aLineThickness );
/**
* @brief Uses DRAWSEGMENT to draw the vertices on mBoard object
* @param aCadstarVertices
* @param aKiCadLayer KiCad layer to draw on
* @param aLineThickness Thickness of line to draw with
*/
void drawCadstarVerticesAsSegments( const std::vector<VERTEX>& aCadstarVertices,
const PCB_LAYER_ID& aKiCadLayer, const int& aLineThickness );
/**
* @brief Returns a vector of pointers to DRAWSEGMENT objects
* @param aCadstarVertices
* @return
*/
std::vector<DRAWSEGMENT *> getDrawSegments( const std::vector<VERTEX>& aCadstarVertices );
/**
* @brief If the LineCode ID is found, returns the thickness as defined in the Linecode,
* otherwise returns the default line thickness in Edge_Cuts KiCad layer
* @param aCadstarLineCodeID
* @return
*/
int getLineThickness( const LINECODE_ID& aCadstarLineCodeID );
/**
* @brief Scales, offsets and inverts y axis to make the point usable directly in KiCad
* @param aCadstarPoint
* @return
*/
wxPoint getKiCadPoint( wxPoint aCadstarPoint );
/**
* @brief
* @param aPoint
* @return Angle in decidegrees of the polar representation of the point, scaled 0..360
*/
double getPolarAngle( wxPoint aPoint );
/**
* @brief
* @param aLayerNum Physical / logical layer number (starts at 1)
* @return PCB_LAYER_ID
*/
PCB_LAYER_ID getKiCadCopperLayerID( unsigned int aLayerNum );
/**
* @brief
* @param aCadstarLayerID
* @return PCB_LAYER_ID
*/
PCB_LAYER_ID getKiCadLayer( const LAYER_ID& aCadstarLayerID );
};