📄 femmviewlua.cpp
字号:
MMnu->CheckMenuItem(ID_MENUSHOWPTS, MF_UNCHECKED);
theView->RedrawView();
return 0;
}
int CFemmviewDoc::lua_gridsnap(lua_State * L)
{
CFemmviewDoc * thisDoc;
CFemmviewView * theView;
thisDoc=(CFemmviewDoc *)pFemmviewdoc;
POSITION pos;
pos=thisDoc->GetFirstViewPosition();
theView=(CFemmviewView *)thisDoc->GetNextView(pos);
CString temp;
temp.Format("%s",lua_tostring(L,1));
temp.MakeLower();
CMainFrame *MFrm;
MFrm=(CMainFrame *)theView->GetTopLevelFrame();
CMenu* MMnu=MFrm->GetMenu();
CToolBar *pToolBar;
pToolBar=&MFrm->m_toolBar;
CToolBarCtrl *tc=&pToolBar->GetToolBarCtrl();
if(temp=="off"){
theView->SnapFlag=FALSE;
MMnu->CheckMenuItem(ID_SNAP_GRID, MF_UNCHECKED);
tc->PressButton(ID_SNAP_GRID,FALSE);
}
else{
theView->SnapFlag=TRUE;
MMnu->CheckMenuItem(ID_SNAP_GRID, MF_CHECKED);
tc->PressButton(ID_SNAP_GRID,TRUE);
}
if (temp !="on" && temp !="off")
{
CString msg="Unknown option for grid snap";
lua_error(L,msg.GetBuffer(1));
return 0;
}
return 0;
}
int CFemmviewDoc::lua_setgrid(lua_State * L)
{
CFemmviewDoc * thisDoc;
CFemmviewView * theView;
thisDoc=(CFemmviewDoc *)pFemmviewdoc;
POSITION pos;
pos=thisDoc->GetFirstViewPosition();
theView=(CFemmviewView *)thisDoc->GetNextView(pos);
CString temp;
int coords=0;
double gridsize;
gridsize=lua_tonumber(L,1);
temp.Format("%s",lua_tostring(L,2));
temp.MakeLower();
if (temp=="cart") coords=0;
if (temp=="polar") coords=1;
if (temp !="cart" && temp !="polar")
{
CString msg="Unknown option for set grid";
lua_error(L,msg.GetBuffer(1));
return 0;
}
theView->GridSize=gridsize;
thisDoc->Coords=coords;
theView->RedrawView();
return 0;
}
int CFemmviewDoc::lua_getprobleminfo(lua_State * L)
{
CFemmviewDoc * thisDoc;
CFemmviewView * theView;
thisDoc=(CFemmviewDoc *)pFemmviewdoc;
POSITION pos;
pos=thisDoc->GetFirstViewPosition();
theView=(CFemmviewView *)thisDoc->GetNextView(pos);
lua_pushnumber(L,thisDoc->ProblemType);
lua_pushnumber(L,thisDoc->Frequency);
return 2;
}
int CFemmviewDoc::lua_savebitmap(lua_State * L)
{
CFemmviewDoc * thisDoc;
CFemmviewView * theView;
thisDoc=(CFemmviewDoc *)pFemmviewdoc;
POSITION pos;
pos=thisDoc->GetFirstViewPosition();
theView=(CFemmviewView *)thisDoc->GetNextView(pos);
CString filename;
filename.Format("%s",lua_tostring(L,1));
RECT r;
CDC tempDC;
CBitmap bitmap;
CDC *pDC=theView->GetDC();
theView->GetClientRect(&r);
tempDC.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC, r.right, r.bottom);
tempDC.SelectObject(&bitmap);
tempDC.Rectangle(0,0,r.right,r.bottom);
theView->OnDraw(&tempDC);
// tempDC.BitBlt(0, 0, r.right, r.bottom, pDC, 0, 0, SRCCOPY);
PBITMAPINFO pbmi;
pbmi=thisDoc->CreateBitmapInfoStruct(theView->m_hWnd,HBITMAP(bitmap));
thisDoc->CreateBMPFile(theView->m_hWnd,filename.GetBuffer(1),pbmi,HBITMAP(bitmap),tempDC.m_hDC);
// to save a bitmap file we need
// a bitmapheader lets use the v5
// OpenClipboard();
// EmptyClipboard();
// SetClipboardData(CF_BITMAP, HBITMAP(bitmap));
// CloseClipboard();
return 0;
}
PBITMAPINFO CFemmviewDoc::CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
// Retrieve the bitmap's color format, width, and height.
if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
{//errhandler("GetObject", hwnd);
AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy25");
}
// Convert the color format to a count of bits.
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1)
cClrBits = 1;
else if (cClrBits <= 4)
cClrBits = 4;
else if (cClrBits <= 8)
cClrBits = 8;
else if (cClrBits <= 16)
cClrBits = 16;
else if (cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;
// Allocate memory for the BITMAPINFO structure. (This structure
// contains a BITMAPINFOHEADER structure and an array of RGBQUAD
// data structures.)
if (cClrBits >= 24)
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * (1<< cClrBits));
// There is no RGBQUAD array for the 24-bit-per-pixel format.
else
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
sizeof(BITMAPINFOHEADER));
// Initialize the fields in the BITMAPINFO structure.
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (cClrBits < 24)
pbmi->bmiHeader.biClrUsed = (1<<cClrBits);
// If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB;
// Compute the number of bytes in the array of color
// indices and store the result in biSizeImage.
// For Windows NT/2000, the width must be DWORD aligned unless
// the bitmap is RLE compressed. This example shows this.
// For Windows 95/98, the width must be WORD aligned unless the
// bitmap is RLE compressed.
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
* pbmi->bmiHeader.biHeight;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
pbmi->bmiHeader.biClrImportant = 0;
return pbmi;
}
void CFemmviewDoc::CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
HBITMAP hBMP, HDC hDC)
{
HANDLE hf; // file handle
BITMAPFILEHEADER hdr; // bitmap file-header
PBITMAPINFOHEADER pbih; // bitmap info-header
LPBYTE lpBits; // memory pointer
DWORD dwTotal; // total count of bytes
DWORD cb; // incremental count of bytes
BYTE *hp; // byte pointer
DWORD dwTmp;
pbih = (PBITMAPINFOHEADER) pbi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
if (!lpBits)
AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy26");
// errhandler("GlobalAlloc", hwnd);
// Retrieve the color table (RGBQUAD array) and the bits
// (array of palette indices) from the DIB.
if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi,
DIB_RGB_COLORS))
{
// errhandler("GetDIBits", hwnd);
AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy27");
}
// Create the .BMP file.
hf = CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE)
{// errhandler("CreateFile", hwnd);
AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy28");
}
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
// Compute the offset to the array of color indices.
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof (RGBQUAD);
// Copy the BITMAPFILEHEADER into the .BMP file.
if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
(LPDWORD) &dwTmp, NULL))
{
AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy29");
// errhandler("WriteFile", hwnd);
}
// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
+ pbih->biClrUsed * sizeof (RGBQUAD),
(LPDWORD) &dwTmp, ( NULL)))
// errhandler("WriteFile", hwnd);
AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy30");
// Copy the array of color indices into the .BMP file.
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
// errhandler("WriteFile", hwnd);
AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy31");
// Close the .BMP file.
if (!CloseHandle(hf))
// errhandler("CloseHandle", hwnd);
AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy32");
// Free memory.
GlobalFree((HGLOBAL)lpBits);
}
int CFemmviewDoc::lua_getcircuitprops(lua_State *L)
{
CFemmviewDoc * TheDoc; // note normally thisdoc
CFemmviewView * theView;
TheDoc=(CFemmviewDoc *)pFemmviewdoc;
POSITION pos;
pos=TheDoc->GetFirstViewPosition();
theView=(CFemmviewView *)TheDoc->GetNextView(pos);
int NumCircuits,k;
CString circuitname;
circuitname=lua_tostring(L,1);
k=-1;
// ok we need to find the correct entry for the circuit name
NumCircuits=TheDoc->circproplist.GetSize();
for(int i=0;i<NumCircuits;i++)
{
if(TheDoc->circproplist[i].CircName==circuitname)
{
k=i;
i=NumCircuits; // that will break it
}
}
// trap errors
if(k==-1)
{
CString msg="Unknown circuit";
lua_error(L,msg.GetBuffer(1));
return 0;
}
CComplex amps,volts,fluxlinkage;
amps =TheDoc->circproplist[k].Amps;
volts=TheDoc->GetVoltageDrop(k);
fluxlinkage=TheDoc->GetFluxLinkage(k);
lua_pushnumber(L,Re(amps));
lua_pushnumber(L,Im(amps));
lua_pushnumber(L,Re(volts));
lua_pushnumber(L,Im(volts));
lua_pushnumber(L,Re(fluxlinkage));
lua_pushnumber(L,Im(fluxlinkage));
return 6;
}
int CFemmviewDoc::lua_saveWMF(lua_State * L)
{
CFemmviewDoc * thisDoc;
CFemmviewView * theView;
thisDoc=(CFemmviewDoc *)pFemmviewdoc;
POSITION pos;
pos=thisDoc->GetFirstViewPosition();
theView=(CFemmviewView *)thisDoc->GetNextView(pos);
CString filename;
filename.Format("%s",lua_tostring(L,1));
RECT r;
CDC tempDC;
CDC *pDC=theView->GetDC();
CMetaFileDC Meta;
CRgn R;
Meta.CreateEnhanced(NULL,NULL,NULL,NULL);
theView->GetClientRect(&r);
R.CreateRectRgnIndirect(&r);
Meta.SelectClipRgn(&R);
theView->OnDraw(&Meta);
HENHMETAFILE hMeta=Meta.CloseEnhanced();
DeleteEnhMetaFile(CopyEnhMetaFile(hMeta,filename));
DeleteEnhMetaFile(hMeta);
return 0;
}
int CFemmviewDoc::lua_refreshview(lua_State * L)
{
CFemmviewDoc * thisDoc;
CFemmviewView * theView;
thisDoc=(CFemmviewDoc *)pFemmviewdoc;
POSITION pos;
pos=thisDoc->GetFirstViewPosition();
theView=(CFemmviewView *)thisDoc->GetNextView(pos);
CDC *pDC=theView->GetDC();
// erase background
CBrush backBrush(theView->BackColor);
CBrush* pOldBrush = pDC->SelectObject(&backBrush);
CRect rect;
pDC->GetClipBox(&rect);
pDC->PatBlt(rect.left, rect.top, rect.Width(),
rect.Height(), PATCOPY);
pDC->SelectObject(pOldBrush);
// redraw the view
theView->OnDraw(pDC);
return 0;
}
int CFemmviewDoc::lua_selectline(lua_State *L)
{
CFemmviewDoc * pDoc; // note normally thisdoc
CFemmviewView * theView;
pDoc=(CFemmviewDoc *)pFemmviewdoc;
POSITION pos;
pos=pDoc->GetFirstViewPosition();
theView=(CFemmviewView *)pDoc->GetNextView(pos);
theView->EditAction=1; // make sure things update OK
double mx,my;
int i,j,k,m;
mx=lua_tonumber(L,1);
my=lua_tonumber(L,2);
//***************
{
if (pDoc->nodelist.GetSize()>0){
i=pDoc->ClosestNode(mx,my);
CComplex x,y,z;
double R,d1,d2;
int lineno,arcno,flag=0;
z.Set(pDoc->nodelist[i].x,pDoc->nodelist[i].y);
if (pDoc->contour.GetSize()>0){
//check to see if point is the same as last point in the contour;
y=pDoc->contour[pDoc->contour.GetSize()-1];
if ((y.re==z.re) && (y.im==z.im)) return 0;
j=pDoc->ClosestNode(y.re,y.im);
x.Set(pDoc->nodelist[j].x,pDoc->nodelist[j].y);
//check to see if this point and the last point are ends of an
//input segment;
lineno=-1;
d1=1.e08;
if (abs(x-y)<1.e-08){
for(k=0;k<pDoc->linelist.GetSize();k++){
if((pDoc->linelist[k].n0==j) && (pDoc->linelist[k].n1==i))
{
d2=fabs(pDoc->ShortestDistanceFromSegment(mx,my,k));
if(d2<d1){
lineno=k;
d1=d2;
}
}
if((pDoc->linelist[k].n0==i) && (pDoc->linelist[k].n1==j))
{
d2=fabs(pDoc->ShortestDistanceFromSegment(mx,my,k));
if(d2<d1){
lineno=k;
d1=d2;
}
}
}
}
//check to see if this point and last point are ends of an
// arc segment; if so, add entire arc to the contour;
arcno=-1;
if (abs(x-y)<1.e-08){
for(k=0;k<pDoc->arclist.GetSize();k++){
if((pDoc->arclist[k].n0==j) && (pDoc->arclist[k].n1==i))
{
d2=pDoc->ShortestDistanceFromArc(CComplex(mx,my),
pDoc->arclist[k]);
if(d2<d1){
arcno=k;
lineno=-1;
flag=TRUE;
d1=d2;
}
}
if((pDoc->arclist[k].n0==i) && (pDoc->arclist[k].n1==j))
{
d2=pDoc->ShortestDistanceFromArc(CComplex(mx,my),
pDoc->arclist[k]);
if(d2<d1){
arcno=k;
lineno=-1;
flag=FALSE;
d1=d2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -