cIFace.c

00001 /* 
00002  Copyright (C) 2008 Endre Bak <endrebak@yahoo.com>
00003 
00004  Licensed under the terms of the GNU General Public License, version
00005  2 or later.
00006 
00007 */
00008 
00009 /*
00010  necessity of the .c interface:
00011  It would be better to only have a single C++ interface for connecting PCB and
00012  the Java plugin. But I had compile errors when I tried to inlcude PCB headers
00013  from C++ even with playing extern "C" includes.
00014 */
00015 
00016 
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 
00020 
00021 #include "cIFace.h"
00022 #include "cppIFace.hpp"
00023 
00024 #include "hid.h"
00025 #include "error.h"
00026 #include "global.h"
00027 #include "data.h"
00028 #include "draw.h"
00029 #include "undo.h"
00030 
00031 /*
00032         see macro.h const.h
00033         ONSOLDERFLAG - Pad should have it
00034 
00035         TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER
00036         int layer = GetLayerNumber (PCB->Data, (LayerTypePtr) ptr1);
00037         const.h:#define SOLDER_LAYER            0
00038         const.h:#define COMPONENT_LAYER         1
00039  */
00040 
00041 void msg(const char * fmt,...) {
00042         va_list ap;
00043         va_start(ap, fmt);
00044         Message(fmt, ap);
00045 }
00046 
00047 int getNetNameN() {
00048         LibraryType lib = PCB->NetlistLib;
00049         return lib.MenuN;
00050 }
00051 
00052 const char * getNetName(int idx) {
00053         LibraryType lib = PCB->NetlistLib;
00054         return lib.Menu[idx].Name;
00055 }
00056 
00057 int getPinNameN(int netNameIdx) {
00058         LibraryMenuType menu = PCB->NetlistLib.Menu[netNameIdx];
00059         return menu.EntryN;
00060 }
00061 
00062 const char * getPinName(int netNameIdx, int pinIdx) {
00063         LibraryMenuType menu = PCB->NetlistLib.Menu[netNameIdx];
00064         LibraryEntryType entry = menu.Entry[pinIdx];
00065         return entry.ListEntry;
00066 }
00067 
00068 #define VEC_INC 32
00069 
00070 typedef struct Vec {
00071         int rawSize, size;
00072         void **data;
00073 } Vec;
00074 
00075 static void vec_init(Vec *vec) {
00076         vec->rawSize = vec->size = 0;
00077         vec->data = NULL;
00078 }
00079 
00080 static void vec_append(Vec *vec, void *ptr) {
00081         if (vec->size == vec->rawSize) {
00082                 vec->rawSize += VEC_INC;
00083                 vec->data = realloc(vec->data, vec->rawSize*sizeof(void*));
00084         }
00085         vec->data[vec->size++] = ptr;
00086 }
00087 
00088 CPin ** getPins() {
00089         Vec pinVec;
00090         vec_init(&pinVec);
00091         ELEMENT_LOOP(PCB->Data); {
00092                 PIN_LOOP(element); {
00093                         CPin * myPin = (CPin*)malloc(sizeof(CPin));
00094                         myPin->eName = ELEMENT_NAME(PCB, element);
00095                         myPin->num = atoi(pin->Number);
00096                         myPin->seq = 0;
00097                         myPin->x = pin->X;
00098                         myPin->y = pin->Y;
00099                         myPin->sideMask = SIDE_BOTH;
00100                         vec_append(&pinVec, myPin);
00101                 } END_LOOP;
00102                 PAD_LOOP(element); {
00103                         char * name = ELEMENT_NAME(PCB, element);
00104                         int num = atoi(pad->Number);
00105                         int sideMask = TEST_FLAG (ONSOLDERFLAG, pad) ? SIDE_SOLDER : SIDE_COMPONENT;
00106                         CPin * myPin = (CPin*)malloc(sizeof(CPin));
00107                         myPin->eName = name;
00108                         myPin->num = num;
00109                         myPin->seq = 0;
00110                         myPin->x = pad->Point1.X;
00111                         myPin->y = pad->Point1.Y;
00112                         myPin->sideMask = sideMask;
00113                         vec_append(&pinVec, myPin);
00114                         myPin = (CPin*)malloc(sizeof(CPin));
00115                         myPin->eName = name;
00116                         myPin->num = num;
00117                         myPin->seq = 1;
00118                         myPin->x = pad->Point2.X;
00119                         myPin->y = pad->Point2.Y;
00120                         myPin->sideMask = sideMask;
00121                         vec_append(&pinVec, myPin);
00122                 } END_LOOP;
00123         } END_LOOP;     
00124         vec_append(&pinVec, NULL);
00125         return (CPin**)pinVec.data;
00126 }
00127 
00128 void freePins(CPin ** pinA) {
00129         int idx = 0;
00130         if (pinA == NULL) return;
00131         while (pinA[idx]) free(pinA[idx++]);
00132         free(pinA);
00133 }
00134 
00135 /* FIXME: this layer mapping may not be right */
00136 #define SIDEMASK(group) \
00137         group == COMPONENT_LAYER ? SIDE_SOLDER : SIDE_COMPONENT;
00138 
00139 CRat ** getRats() {
00140         Vec ratVec;
00141         vec_init(&ratVec);
00142         RAT_LOOP(PCB->Data); {
00143                 CRat * myRat = (CRat*)malloc(sizeof(CRat));
00144                 myRat->id = n;
00145                 myRat->x0 = line->Point1.X;
00146                 myRat->y0 = line->Point1.Y;
00147                 myRat->sideMask0 = SIDEMASK(line->group1);
00148                 myRat->x1 = line->Point2.X;
00149                 myRat->y1 = line->Point2.Y;
00150                 myRat->sideMask1 = SIDEMASK(line->group2);
00151                 vec_append(&ratVec, myRat);
00152         } END_LOOP;
00153         vec_append(&ratVec, NULL);
00154         return (CRat**)ratVec.data;
00155 }
00156 
00157 void freeRats(CRat ** ratA) {
00158         int idx = 0;
00159         if (ratA == NULL) return;
00160         while (ratA[idx]) free(ratA[idx++]);
00161         free(ratA);
00162 }
00163 
00164 void selectRat(int idx) {
00165         /*
00166         type = SearchScreen (Crosshair.X, Crosshair.Y, SELECT_TYPES,
00167                        &ptr1, &ptr2, &ptr3);
00168          */
00169         /* FIXME is ptr1 a layer? */
00170         RatTypePtr rat = &(PCB->Data)->Rat[idx];
00171         /* TODO: add undo FIXME: how? */
00172         /* AddObjectToFlagUndoList (RATLINE_TYPE, ptr1, ptr1, ptr1); */
00173         TOGGLE_FLAG (SELECTEDFLAG, rat);
00174         DrawRat (rat, 0);
00175 }
00176 
00177 static int ratSel(int argc, char **argv, int x, int y) {
00178         Message("C:RatSel called\n");
00179         ratSelCpp(argc, (const char**)argv, x, y);
00180         return 0;
00181 }
00182 
00183 static HID_Action ratSelActionList[] = {
00184         { "RatSel", NULL, ratSel, NULL, NULL }
00185 };
00186 
00187 REGISTER_ACTIONS (ratSelActionList)
00188 
00189 void pcb_plugin_init() {
00190         register_ratSelActionList();
00191         Message("cIFace.c:RatSel plugin loaded!\n");
00192         initJava();
00193 }

Generated on Tue Aug 17 15:28:04 2010 for pcb-plugins by  doxygen 1.4.6-NO