home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 March
/
Chip_1998-03_cd.bin
/
tema
/
jpcad
/
ADK
/
SAMPLES
/
DLINE
/
DLINE.CP_
/
DLINE.CP
Wrap
Text File
|
1998-01-21
|
8KB
|
291 lines
/* 090896
DLINE.CPP (c) 1996, ANTEK CS
Description:
Sample file for demonstrating the use of ADK (Amoeba Development Kit).
Draws double lines with chosen width.
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
WARRANTY. ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR
PURPOSE AND OF MERCHANTABILITY ARE HEREBY DISCLAIMED.
*/
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <adk.h>
#define SQR(x) ((x) * (x))
#define EVNAMES_ENTITIES_COLOR "COLOR"
#define EVNAMES_ENTITIES_LINETYPE "LINETYPE"
const double EPSILON = 1E-9; // smallest distance between start and end point
const char APP_NAME[] = "Dline";
static HINSTANCE g_hInst;
static void ProcedureCall(long);
static void Dline(void);
static int DrawDoubleLine(A_PointS, A_PointS, double, A_PointS, A_EntH*, A_EntH*);
long A_CALLBACK DragLine(void FAR*, A_PointS, A_MatrixS FAR*);
void A_CALLBACK Status(long,long);
void A_CALLBACK Error(long);
///////////////////////////////////////////////////////////////////////////////////////////////
#ifndef A_16BIT
int WINAPI WinMain(
HINSTANCE,
HINSTANCE,
LPSTR,
int)
{
long lReturn = A_Adk(A_VERSION,Status,Error);
if (lReturn!=A_ERROR_OK)
{
char Buffer[128];
sprintf(Buffer, "Failed to initiate communication with JPCAD (0x%lX)", lReturn);
MessageBox(NULL,Buffer, APP_NAME, MB_ICONSTOP);
return -1;
}
return 0;
}
#else
int PASCAL WinMain(
HINSTANCE hInstance,
HINSTANCE,
LPSTR,
int)
{
g_hInst = hInstance;
long lReturn;
A_StatusF FAR*lpfnStatus = (A_StatusF FAR*)MakeProcInstance ((FARPROC) Status, g_hInst);
A_ErrorF FAR*lpfnError = (A_ErrorF FAR*)MakeProcInstance ((FARPROC) Error, g_hInst);
if ((lReturn = A_Adk(A_VERSION,lpfnStatus,lpfnError))!=A_ERROR_OK)
{
char Buffer[128];
sprintf(Buffer, "Failed to initiate communication with JPCAD (0x%lX)", lReturn);
MessageBox(NULL,Buffer, APP_NAME, MB_ICONSTOP);
}
FreeProcInstance((FARPROC)(lpfnStatus));
FreeProcInstance((FARPROC)(lpfnError));
return 0;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////
void A_CALLBACK Error(long nErrorCode)
{
char s[80];
sprintf(s,"\nError code %d",nErrorCode);
A_Prompt(s);
}
///////////////////////////////////////////////////////////////////////////////////////////////
void A_CALLBACK Status(long nStatus, long nCmdID)
{
switch(nStatus)
{
case A_LOAD:
A_Prompt("\nDLINE - defining new command.");
A_DefCmd("dline", 0, FALSE);
break;
case A_CMD_CALL:
// procedure was called
ProcedureCall(nCmdID);
break;
case A_UNLOAD:
// unloading was requested
A_Prompt("\nDLINE - unloading.");
return;
default:
// unknown status code
break;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
static void ProcedureCall(long CmdID)
{
switch(CmdID)
// launch procedure with given code
{
case 0:
Dline();
break;
default:
A_Prompt("Bad procedure code!");
break;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
long A_CALLBACK DragLine(void FAR*Start, A_PointS End, A_MatrixS FAR*n)
{
A_DrawLine(*(A_PointS FAR*)Start,End);
return 1;
};
///////////////////////////////////////////////////////////////////////////////////////////////
static void Dline(void)
{
double DlineWidth = 10.0;
BOOL IsPrevLine=FALSE;
A_PointS StartPoint,EndPoint,LineVect;
A_PointS L1StartPoint,L2StartPoint,L1EndPoint,L2EndPoint,Vector,PrevVector,Vector1,Vector2;
A_VectorS PVector,V;
A_PointS S1,S2,E1,E2,Intersect1, Intersect2;
A_EntH PrevLine1,PrevLine2;
double pWidth,t1,t2;
long result;
// Get line width or set it as 10.0 if the user chooses default
long retvalue;
retvalue=A_GetDouble("Width ",NULL,"10.0",&DlineWidth);
if (retvalue==A_GET_CANCEL)
return;
else
if (retvalue==A_GET_DEFAULT)
DlineWidth=10.0;
// Get first point of DLINE
if (A_GetPoint("First dline point",NULL,NULL,&StartPoint)==A_GET_CANCEL)
return;
#ifdef A_16BIT
A_GetDragF *lpfnDragLine = (A_GetDragF*)MakeProcInstance ((FARPROC) DragLine, g_hInst);
#endif
for(;;)
// while there are any points...
{
result = A_GetPointDrag("Next dline point",NULL,"",A_A_EMPTY,&StartPoint,
#ifdef A_16BIT
lpfnDragLine,
#else
DragLine,
#endif
&StartPoint,&EndPoint);
if ((result==A_GET_CANCEL)||(result==A_GET_DEFAULT))
break;
LineVect=A_G_SubVV(StartPoint,EndPoint);
if(A_G_LenV(LineVect)<EPSILON)
{
continue;
};
Vector=A_G_SubVV(StartPoint,EndPoint);
PVector=A_G_PerpenV(Vector,1);
V=A_G_MulVR(A_G_NormV(PVector),DlineWidth/2);
S1=A_G_AddVV(StartPoint,V);
E1=A_G_AddVV(EndPoint,V);
PVector=A_G_PerpenV(Vector,0);
V=A_G_MulVR(A_G_NormV(PVector),DlineWidth/2);
S2=A_G_AddVV(StartPoint,V);
E2=A_G_AddVV(EndPoint,V);
if (IsPrevLine)
{
A_LINE_Get(PrevLine1,&L1StartPoint,&L1EndPoint,
/*layer*/NULL,/*color*/NULL,/*ltype*/NULL,&pWidth);
A_LINE_Get(PrevLine2,&L2StartPoint,&L2EndPoint,
/*layer*/NULL,/*color*/NULL,/*ltype*/NULL,&pWidth);
Vector1=A_G_SubVV(S1,E1);
// compute intersection of first lines...
if (A_G_IntersLL(L1StartPoint,A_G_SubVV(L1StartPoint,L1EndPoint),
S1,Vector1,&t1,&t2)==0)
{
Intersect1 = A_G_AddVV(A_G_MulVR(A_G_SubVV(L1StartPoint,L1EndPoint),t1),L1StartPoint);
// change previous first line so as to end on the intersection point
A_LINE_Change(PrevLine1,L1StartPoint,Intersect1,
A_USE_CURRENT_ENT,// layer
A_USE_CURRENT,// color
A_USE_CURRENT,// ltype
pWidth);
}
// ... or let it be the ending point of the previous first line
else
{
A_PointS v,w;
v = A_G_NormV(PrevVector);
w = A_G_NormV(Vector);
if (A_G_SMulVV(v,w)<0)
Intersect1=L2EndPoint;
else
Intersect1=L1EndPoint;
};
// draw first line
A_LINE_Make(Intersect1,E1,A_USE_CURRENT_ENT,A_USE_CURRENT,A_USE_CURRENT,0,FALSE,&PrevLine1);
Vector2=A_G_SubVV(S2,E2);
// compute intersection of second lines...
if (A_G_IntersLL(L2StartPoint,A_G_SubVV(L2StartPoint,L2EndPoint),
S2,Vector2,&t1,&t2)==0)
{
Intersect2 = A_G_AddVV(A_G_MulVR(A_G_SubVV(L2StartPoint,L2EndPoint),t1),L2StartPoint);
// change previous second line so as to end on intersection point
A_LINE_Change(PrevLine2,L2StartPoint,Intersect2,
A_USE_CURRENT_ENT,// layer
A_USE_CURRENT,// color
A_USE_CURRENT,// ltype
pWidth);
}
// ... or let it be the ending point of the previous second line
else
{
A_PointS v,w;
v = A_G_NormV(PrevVector);
w = A_G_NormV(Vector);
if (A_G_SMulVV(v,w)<0)
Intersect2=L1EndPoint;
else
Intersect2=L2EndPoint;
};
// draw second line
A_LINE_Make(Intersect2,E2,A_USE_CURRENT_ENT,A_USE_CURRENT,A_USE_CURRENT,0,FALSE,&PrevLine2);
}
else
{
// draw first line
if (A_LINE_Make(S1,E1,A_USE_CURRENT_ENT,A_USE_CURRENT,A_USE_CURRENT,0,FALSE,&PrevLine1)<0)
{
A_Prompt("Error drawing line 1!");
}
// draw second line
if (A_LINE_Make(S2,E2,A_USE_CURRENT_ENT,A_USE_CURRENT,A_USE_CURRENT,0,FALSE,&PrevLine2)<0)
{
A_Prompt("Error drawing line 2!");
};
}
StartPoint=EndPoint;
PrevVector=Vector;
IsPrevLine=TRUE;
// end while
}
#ifdef A_16BIT
FreeProcInstance((FARPROC)(lpfnDragLine));
#endif
};