common_plot_functions.cpp 8.97 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
		/******************************************/
		/* Kicad: Common plot Postscript Routines */
		/******************************************/

#include "fctsys.h"
#include "gr_basic.h"
#include "trigo.h"
#include "wxstruct.h"
#include "base_struct.h"
#include "common.h"
#include "plot_common.h"
#include "worksheet.h"
#include "macros.h"


// Variables partagees avec Common plot Postscript et HPLG Routines
wxPoint LastPenPosition;
wxPoint PlotOffset;
FILE * PlotOutputFile;
double XScale, YScale;
int g_DefaultPenWidth;
int g_CurrentPenWidth = -1;
int PlotOrientOptions, etat_plume;


// Locales
static Ki_PageDescr * SheetPS;

/*************************/
void ForcePenReinit(void)
/*************************/
/* set the flag g_CurrentPenWidth to -1 in order to force a pen width redefinition
	for the next draw command
*/
{
	g_CurrentPenWidth = -1;
}

/**********************************************/
void SetPlotScale(double xscale, double yscale)
/**********************************************/
/* Set the plot scale for the current plotting)
*/
{
	XScale = xscale;
	YScale = yscale;
}

/*********************************/
void SetPlotOffset(wxPoint offset)
/*********************************/
/* Set the plot offset for the current plotting)
*/
{
	PlotOffset = offset;
}

/***************************************************************************/
void InitPlotParametresGERBER(wxPoint offset, double xscale, double yscale)
/***************************************************************************/
/* Set the plot offset for the current plotting
 xscale,yscale = coordinate scale (scale coefficient for coordinates)
*/
{
	PlotOrientOptions = 0;
	PlotOffset = offset;
	SheetPS = NULL;
	XScale = xscale;
	YScale = yscale;
	g_DefaultPenWidth = 120;			/* epaisseur du trait standard en 1/1000 pouce */
	g_CurrentPenWidth = -1;
}


/*******************************************************/
void PlotWorkSheet(int format_plot, BASE_SCREEN * screen)
/*******************************************************/
/* Plot sheet references
	margin is in mils (1/1000 inch)
*/
{
#define WSTEXTSIZE 50	// Text size in mils
Ki_PageDescr * Sheet = screen->m_CurrentSheet;
int ii, jj, xg , yg, ipas, gxpas, gypas;
wxSize PageSize;
wxPoint pos, ref;
int color;
Ki_WorkSheetData * WsItem;
int conv_unit = screen->GetInternalUnits()/1000;	/* Scale to convert dimension in 1/1000 in into internal units
				(1/1000 inc for EESchema, 1/10000 for pcbnew */
wxString msg;
wxSize text_size;
void (*FctPlume)(wxPoint pos, int state);
int UpperLimit = VARIABLE_BLOCK_START_POSITION;

	switch ( format_plot)
	{
		case PLOT_FORMAT_POST:
			FctPlume = LineTo_PS;
			break;

		case PLOT_FORMAT_HPGL:
			FctPlume = Move_Plume_HPGL;
			break;

		case PLOT_FORMAT_GERBER:
		default:
			return;
	}

	color = BLACK;
	
	PageSize.x = Sheet->m_Size.x;
	PageSize.y = Sheet->m_Size.y;

	/* trace de la bordure */
	ref.x = Sheet->m_LeftMargin * conv_unit;
	ref.y = Sheet->m_TopMargin * conv_unit;		/* Upper left corner */
	xg = (PageSize.x - Sheet->m_RightMargin) * conv_unit;
	yg = (PageSize.y - Sheet->m_BottomMargin) * conv_unit;	/* lower right corner */

	for ( ii = 0; ii < 2 ; ii++ )
	{
		FctPlume(ref,'U');
		pos.x = xg; pos.y = ref.y;
		FctPlume(pos,'D');
		pos.x = xg; pos.y = yg;
		FctPlume(pos,'D');
		pos.x = ref.x; pos.y = yg;
		FctPlume( pos,'D' );
		FctPlume(ref,'D');
		ref.x += GRID_REF_W * conv_unit; ref.y += GRID_REF_W * conv_unit;
		xg -= GRID_REF_W * conv_unit; yg -= GRID_REF_W * conv_unit;
	}

	/* trace des reperes */
	text_size.x = WSTEXTSIZE  * conv_unit;
	text_size.y = WSTEXTSIZE  * conv_unit;

	ref.x = Sheet->m_LeftMargin;
	ref.y = Sheet->m_TopMargin;			/* Upper left corner in 1/1000 inch */
	xg = (PageSize.x - Sheet->m_RightMargin);
	yg = (PageSize.y - Sheet->m_BottomMargin);	/* lower right corner in 1/1000 inch */

	/* Trace des reperes selon l'axe X */
	ipas = (xg - ref.x) / PAS_REF;
	gxpas = ( xg - ref.x) / ipas;
	for ( ii = ref.x + gxpas, jj = 1; ipas > 0 ; ii += gxpas , jj++, ipas--)
	{
		msg.Empty(); msg << jj;
		if( ii < xg - PAS_REF/2 )
		{
			pos.x = ii * conv_unit; pos.y = ref.y * conv_unit;
			FctPlume(pos, 'U');
			pos.x = ii * conv_unit; pos.y = (ref.y + GRID_REF_W) * conv_unit;
			FctPlume(pos,'D');
		}
		pos.x = (ii - gxpas/2) * conv_unit;
		pos.y = (ref.y + GRID_REF_W/2)  * conv_unit;
		PlotGraphicText(format_plot, pos, color,
					msg, TEXT_ORIENT_HORIZ,text_size,
					GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER);

		if( ii < xg - PAS_REF/2 )
		{
			pos.x = ii * conv_unit; pos.y = yg * conv_unit;
			FctPlume(pos,'U');
			pos.x = ii * conv_unit; pos.y = (yg - GRID_REF_W) * conv_unit;
			FctPlume(pos,'D');
		}
		pos.x = (ii - gxpas/2) * conv_unit;
		pos.y = (yg - GRID_REF_W/2)  * conv_unit;
		PlotGraphicText(format_plot, pos, color,
					msg, TEXT_ORIENT_HORIZ,text_size,
					GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER);
	}

	/* Trace des reperes selon l'axe Y */
	ipas = (yg - ref.y) / PAS_REF;
	gypas = ( yg - ref.y) / ipas;
	for ( ii = ref.y + gypas, jj = 0; ipas > 0 ; ii += gypas , jj++, ipas--)
	{
		msg.Empty(); msg << jj;
		if( ii < yg - PAS_REF/2 )
		{
			pos.x = ref.x * conv_unit; pos.y = ii * conv_unit;
			FctPlume(pos,'U');
			pos.x = (ref.x + GRID_REF_W) * conv_unit; pos.y = ii * conv_unit;
			FctPlume(pos, 'D');
		}
		pos.x = (ref.x + GRID_REF_W/2) * conv_unit;
		pos.y = (ii - gypas/2) * conv_unit;
		PlotGraphicText(format_plot, pos, color,
					msg, TEXT_ORIENT_HORIZ,text_size,
					GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER);

		if( ii < yg - PAS_REF/2 )
		{
			pos.x = xg * conv_unit; pos.y = ii * conv_unit;
			FctPlume(pos,'U');
			pos.x = (xg - GRID_REF_W) * conv_unit; pos.y = ii * conv_unit;
			FctPlume(pos,'D');
		}
		pos.x = (xg - GRID_REF_W/2) * conv_unit;
		pos.y = (ii - gypas/2) * conv_unit;
		PlotGraphicText(format_plot, pos, color, msg, TEXT_ORIENT_HORIZ,text_size,
					GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER);
	}

	/* Trace du cartouche */
	text_size.x = SIZETEXT  * conv_unit;
	text_size.y = SIZETEXT  * conv_unit;
	ref.x = PageSize.x - GRID_REF_W - Sheet->m_RightMargin;
	ref.y = PageSize.y - GRID_REF_W - Sheet->m_BottomMargin;

	for( WsItem = &WS_Date; WsItem != NULL; WsItem = WsItem->Pnext )
	{
		pos.x = (ref.x - WsItem->m_Posx) * conv_unit;
		pos.y = (ref.y - WsItem->m_Posy) * conv_unit;
		if(WsItem->m_Legende) msg = WsItem->m_Legende;
		else msg.Empty();
		switch( WsItem->m_Type )
		{
			case WS_DATE:
				msg += screen->m_Date;
				break;

			case WS_REV:
				msg += screen->m_Revision;
				break;

			case WS_LICENCE:
				msg += g_ProductName;
				break;

			case WS_SIZESHEET:
				msg += screen->m_CurrentSheet->m_Name;
				break;

			case WS_IDENTSHEET:
				msg << screen->m_SheetNumber << wxT("/") << screen->m_NumberOfSheet;
				break;

			case WS_COMPANY_NAME:
				msg += screen->m_Company;
				if ( ! msg.IsEmpty() )
					UpperLimit = MAX(UpperLimit, WsItem->m_Posy+SIZETEXT);
				break;

			case WS_TITLE:
				msg += screen->m_Title;
				break;

			case WS_COMMENT1:
				msg += screen->m_Commentaire1;
				if ( ! msg.IsEmpty() )
					UpperLimit = MAX(UpperLimit, WsItem->m_Posy+SIZETEXT);
				break;

			case WS_COMMENT2:
				msg += screen->m_Commentaire2;
				if ( ! msg.IsEmpty() )
					UpperLimit = MAX(UpperLimit, WsItem->m_Posy+SIZETEXT);
				break;

			case WS_COMMENT3:
				msg += screen->m_Commentaire3;
				if ( ! msg.IsEmpty() )
					UpperLimit = MAX(UpperLimit, WsItem->m_Posy+SIZETEXT);
				break;

			case WS_COMMENT4:
				msg += screen->m_Commentaire4;
				if ( ! msg.IsEmpty() )
					UpperLimit = MAX(UpperLimit, WsItem->m_Posy+SIZETEXT);
				break;

			case WS_UPPER_SEGMENT:
				if (UpperLimit == 0 ) break;
			case WS_LEFT_SEGMENT:
				WS_MostUpperLine.m_Posy = 
				WS_MostUpperLine.m_Endy = 
				WS_MostLeftLine.m_Posy = UpperLimit;
				pos.y = (ref.y - WsItem->m_Posy) * conv_unit;
			case WS_SEGMENT:
			{
				wxPoint auxpos;
				auxpos.x = (ref.x - WsItem->m_Endx) * conv_unit;;
				auxpos.y = (ref.y - WsItem->m_Endy) * conv_unit;;
				FctPlume(pos, 'U');
				FctPlume(auxpos, 'D');
			}
				break;

		}
		if ( ! msg.IsEmpty() )
		{
			PlotGraphicText(format_plot, pos, color,
					msg.GetData(), TEXT_ORIENT_HORIZ,text_size,
					GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_VJUSTIFY_CENTER);
		}
	}

	switch ( format_plot )
		{
		case PLOT_FORMAT_HPGL:
			Plume_HPGL('U');
			break;
		case PLOT_FORMAT_POST:
			break;
		}
}


/******************************************/
void UserToDeviceCoordinate(wxPoint & pos )
/******************************************/
/* modifie les coord pos.x et pos.y pour le trace selon l'orientation,
	l'echelle, les offsets de trace */
{

	pos.x = (int) (pos.x * XScale);
	pos.y = (int) (pos.y * YScale);

	switch ( PlotOrientOptions) /* Calcul du cadrage */
		{
		default :
			pos.x -= PlotOffset.x ; pos.y = PlotOffset.y - pos.y;
			break ;

		case PLOT_MIROIR :
			pos.x -= PlotOffset.x ; pos.y = - PlotOffset.y + pos.y;
			break ;

		}
}

/************************************/
void UserToDeviceSize(wxSize & size )
/************************************/
/* modifie les dimension size.x et size.y pour le trace selon l'echelle */
{
	size.x = (int) (size.x * XScale);
	size.y = (int) (size.y * YScale);
}