⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 femmviewdoc.cpp

📁 一个2D电磁场FEM计算的VC++源程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// femmviewDoc.cpp : implementation of the CFemmviewDoc class
//


#include "stdafx.h"
#include <afx.h>
#include <afxtempl.h>
#include "problem.h"
#include "femmview.h"
#include "xyplot.h"
#include "MainFrm.h"
#include "ptloc.h"
#include "femmviewDoc.h"
#include "femmviewView.h"
#include "lua.h"

extern lua_State * lua;
extern CString luascriptname;
extern CFemmviewApp theApp;
extern BOOL bLinehook;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFemmviewDoc

IMPLEMENT_DYNCREATE(CFemmviewDoc, CDocument)

BEGIN_MESSAGE_MAP(CFemmviewDoc, CDocument)
	//{{AFX_MSG_MAP(CFemmviewDoc)
	ON_COMMAND(ID_FILE_OPEN_LUA_SCRIPT, OnFileOpenLuaScript)
	//}}AFX_MSG_MAP
	
	ON_COMMAND(ID_VIEW_LUACONSOLE, OnViewLuaConsole)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CFemmviewDoc construction/destruction

double sqr(double x)
{
	return x*x;
}

CFemmviewDoc::CFemmviewDoc()
{
	// set some default values for problem definition
	d_LineIntegralPoints=400;
	d_ShiftH=FALSE;
	d_luaconsole=FALSE;
	Frequency=0.;
	Depth=1/0.0254;
	LengthUnits=0;
	ProblemType=FALSE;
	ProblemNote="Add comments here.";
	FirstDraw=-1;
	A_High=0.;
	A_Low=0.;
	A_lb=0.;
	A_ub=0.;
	extRo=extRi=extZo=0;
	Smooth=TRUE;
	NumList=NULL;
	ConList=NULL;
	bHasMask=FALSE;
	LengthConv=(double *)calloc(6,sizeof(double));
	LengthConv[0]=0.0254;	//inches
	LengthConv[1]=0.001;	//millimeters
	LengthConv[2]=0.01;		//centimeters
	LengthConv[3]=1.;		//meters
	LengthConv[4]=2.54e-05; //mils
	LengthConv[5]=1.e-06;	//micrometers
	Coords=FALSE;
	
	// determine path to bin directory
	BinDir=theApp.GetExecutablePath();

	ScanPreferences();

	// lua initialization stuff
	initalise_lua();
}

CFemmviewDoc::~CFemmviewDoc()
{
	int i;
	free(LengthConv);
	for(i=0;i<meshnode.GetSize();i++)
		if(ConList[i]!=NULL) free(ConList[i]);
	free(ConList);
	free(NumList);
}

BOOL CFemmviewDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	int i;

	// clear out all current lines, nodes, and block labels
	nodelist.RemoveAll();
	linelist.RemoveAll();
	blocklist.RemoveAll();
	arclist.RemoveAll();
	nodeproplist.RemoveAll();
	lineproplist.RemoveAll();
	blockproplist.RemoveAll();
	circproplist.RemoveAll();
	meshnode.RemoveAll();
	meshelem.RemoveAll();
	contour.RemoveAll();
	if(NumList!=NULL)
	{
		for(i=0;i<meshnode.GetSize();i++)
			if(ConList[i]!=NULL) free(ConList[i]);
		free(ConList); ConList=NULL;
		free(NumList); NumList=NULL;
	}

	// set problem attributes to generic ones;
	Frequency=0.;
	LengthUnits=0;
	ProblemType=FALSE;
	ProblemNote="Add comments here.";
	bHasMask=FALSE;
	extRo=extRi=extZo=0;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CFemmviewDoc serialization

void CFemmviewDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CFemmviewDoc diagnostics

#ifdef _DEBUG
void CFemmviewDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CFemmviewDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFemmviewDoc commands
char* StripKey(char *c)
{
    char *d;
    int i,k;

    k=strlen(c);

    for(i=0;i<k;i++){
        if (c[i] == '='){
            d=c+i+1;
            return d;
        }
    }

    return c+k;
}

BOOL CFemmviewDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
		
	FILE *fp;
	int i,j,k,t;
	char s[1024],q[1024];
	char *v;
	double b,bi,br;
	double zr,zi;
	BOOL flag=FALSE;
	CPointProp	  PProp;
	CBoundaryProp BProp;
	CMaterialProp MProp;
	CCircuit      CProp;
	CNode		node;
	CSegment	segm;
	CArcSegment asegm;
	CElement	elm;
	CBlockLabel blk;
	CMeshNode	mnode;
	CPoint		mline;

	// make sure old document is cleared out...
	if(NumList!=NULL){
		for(i=0;i<meshnode.GetSize();i++)
			if(ConList[i]!=NULL) free(ConList[i]);
		free(ConList); ConList=NULL;
		free(NumList); NumList=NULL;
	}
	nodelist.RemoveAll();		
	linelist.RemoveAll();
	blocklist.RemoveAll();
	arclist.RemoveAll();
	nodeproplist.RemoveAll();
	lineproplist.RemoveAll();
	blockproplist.RemoveAll();
	circproplist.RemoveAll();
	meshnode.RemoveAll();
	meshelem.RemoveAll();
	contour.RemoveAll();

	if ((fp=fopen(lpszPathName,"rt"))==NULL){
		AfxMessageBox("Couldn't read from specified .poly file");
		return FALSE;
	}

	// define some defaults
	Frequency=0.;
	ProblemType=0;
	Coords=0;
	ProblemNote="";
	bHasMask=FALSE;
	Depth=-1;

	// parse the file
	while (flag==FALSE)
	{
		fgets(s,1024,fp);
		sscanf(s,"%s",q);

		// Deal with flag for file format version
		if( _strnicmp(q,"[format]",8)==0 ){
			double vers;
			v=StripKey(s);
			sscanf(v,"%lf",&vers); vers = 10.*vers + 0.5;
			if( ((int) vers)!=40 ){
				AfxMessageBox("This file is from a different version of FEMM\nRe-analyze the problem using the current version.");
				fclose(fp);
				return FALSE;
			}
			q[0]=NULL;
		}

		// Frequency of the problem
		if( _strnicmp(q,"[frequency]",11)==0){
			v=StripKey(s);
			sscanf(v,"%lf",&Frequency);
			q[0]=NULL;
		}
	
		// Frequency of the problem
		if( _strnicmp(q,"[depth]",7)==0){
			v=StripKey(s);
			sscanf(v,"%lf",&Depth);
			q[0]=NULL;
		}


		// Units of length used by the problem
		if( _strnicmp(q,"[lengthunits]",13)==0){
			v=StripKey(s);
			sscanf(v,"%s",q);
			if( _strnicmp(q,"inches",6)==0) LengthUnits=0;
			else if( _strnicmp(q,"millimeters",11)==0) LengthUnits=1;
			else if( _strnicmp(q,"centimeters",1)==0) LengthUnits=2;
			else if( _strnicmp(q,"mils",4)==0) LengthUnits=4;
			else if( _strnicmp(q,"microns",6)==0) LengthUnits=5;
			else if( _strnicmp(q,"meters",6)==0) LengthUnits=3;
			q[0]=NULL;
		}

		// Problem Type (planar or axisymmetric)
		if( _strnicmp(q,"[problemtype]",13)==0){
			v=StripKey(s);
			sscanf(v,"%s",q);
			if( _strnicmp(q,"planar",6)==0) ProblemType=0;
			if( _strnicmp(q,"axisymmetric",3)==0) ProblemType=1;
			q[0]=NULL;
		}

		// Coordinates (cartesian or polar)
		if( _strnicmp(q,"[coordinates]",13)==0){
			v=StripKey(s);
			sscanf(v,"%s",q);
			if ( _strnicmp(q,"cartesian",4)==0) Coords=0;
			if ( _strnicmp(q,"polar",5)==0) Coords=1;
			q[0]=NULL;
		}
	
		// Comments
		if (_strnicmp(q,"[comment]",9)==0){
			v=StripKey(s);
			// put in carriage returns;
			k=strlen(v);
			for(i=0;i<k;i++)
				if((v[i]=='\\') && (v[i+1]=='n')){
					v[i]=13;
					v[i+1]=10;
				}

			for(i=0;i<k;i++)
				if(v[i]=='\"'){
					v=v+i+1;
					i=k;
				}
			k=strlen(v);
			if(k>0) for(i=k-1;i>=0;i--){
				if(v[i]=='\"'){
					v[i]=0;
					i=-1;
				}
			}
			ProblemNote=v;
			q[0]=NULL;
		}
		
		// properties for axisymmetric external region
		if( _strnicmp(q,"[extzo]",7)==0){
			v=StripKey(s);
			sscanf(v,"%lf",&extZo);
			q[0]=NULL;
		}

		if( _strnicmp(q,"[extro]",7)==0){
			v=StripKey(s);
			sscanf(v,"%lf",&extRo);
			q[0]=NULL;
		}

		if( _strnicmp(q,"[extri]",7)==0){
			v=StripKey(s);
			sscanf(v,"%lf",&extRi);
			q[0]=NULL;
		}

		// Point Properties
		if( _strnicmp(q,"<beginpoint>",11)==0){	
			PProp.PointName="New Point Property";
			PProp.Jr=0.;
			PProp.Ji=0.;
			PProp.Ar=0.;
			PProp.Ai=0.;
			q[0]=NULL;
		}

		if( _strnicmp(q,"<pointname>",11)==0){
			v=StripKey(s);
			k=strlen(v);
			for(i=0;i<k;i++)
				if(v[i]=='\"'){
					v=v+i+1;
					i=k;
				}
			k=strlen(v);
			if(k>0) for(i=k-1;i>=0;i--){
				if(v[i]=='\"'){
					v[i]=0;
					i=-1;
				}
			}
			PProp.PointName=v;
			q[0]=NULL;
		}

		if( _strnicmp(q,"<A_re>",6)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&PProp.Ar);
		   q[0]=NULL;
		}	

		if( _strnicmp(q,"<A_im>",6)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&PProp.Ai);
		   q[0]=NULL;
		}
	
		if( _strnicmp(q,"<I_re>",6)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&PProp.Jr);
		   q[0]=NULL;
		}	

		if( _strnicmp(q,"<I_im>",6)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&PProp.Ji);
		   q[0]=NULL;
		}

		if( _strnicmp(q,"<endpoint>",9)==0){
			nodeproplist.Add(PProp);
			q[0]=NULL;
		}

		// Boundary Properties;
		if( _strnicmp(q,"<beginbdry>",11)==0){	
			BProp.BdryName="New Boundary";
			BProp.BdryFormat=0;
			BProp.A0=0.;
			BProp.A1=0.;
			BProp.A2=0.;
			BProp.phi=0.;	
			BProp.Mu=0.;
			BProp.Sig=0.;
			BProp.c0=0.;
			BProp.c1=0.;
			q[0]=NULL;
		}

		if( _strnicmp(q,"<bdryname>",10)==0){
			v=StripKey(s);
			k=strlen(v);
			for(i=0;i<k;i++)
				if(v[i]=='\"'){
					v=v+i+1;
					i=k;
				}
			k=strlen(v);
			if(k>0) for(i=k-1;i>=0;i--){
				if(v[i]=='\"'){
					v[i]=0;
					i=-1;
				}
			}
			BProp.BdryName=v;
			q[0]=NULL;
		}

		if( _strnicmp(q,"<bdrytype>",10)==0){
		   v=StripKey(s);
		   sscanf(v,"%i",&BProp.BdryFormat);
		   q[0]=NULL;
		}
		
		if( _strnicmp(q,"<mu_ssd>",8)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&BProp.Mu);
		   q[0]=NULL;
		}	

		if( _strnicmp(q,"<sigma_ssd>",11)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&BProp.Sig);
		   q[0]=NULL;
		}	

		if( _strnicmp(q,"<A_0>",5)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&BProp.A0);
		   q[0]=NULL;
		}	

		if( _strnicmp(q,"<A_1>",5)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&BProp.A1);
		   q[0]=NULL;
		}	
		
		if( _strnicmp(q,"<A_2>",5)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&BProp.A2);
		   q[0]=NULL;
		}	

		if( _strnicmp(q,"<phi>",5)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&BProp.phi);
		   q[0]=NULL;
		}	
		
		if( _strnicmp(q,"<c0>",4)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&BProp.c0);
		   q[0]=NULL;
		}	
		
		if( _strnicmp(q,"<c1>",4)==0){
		   v=StripKey(s);
		   sscanf(v,"%lf",&BProp.c1);
		   q[0]=NULL;
		}	

		if( _strnicmp(q,"<endbdry>",9)==0){
			lineproplist.Add(BProp);
			q[0]=NULL;
		}


		// Block Properties;
		if( _strnicmp(q,"<beginblock>",12)==0){	
			MProp.BlockName="New Material";
			MProp.mu_x=1.;
			MProp.mu_y=1.;			// permeabilities, relative
			MProp.H_c=0.;				// magnetization, A/m
			MProp.Jr=0.;
			MProp.Ji=0.;				// applied current density, MA/m^2
			MProp.Cduct=0.;		    // conductivity of the material, MS/m
			MProp.Lam_d=0.;			// lamination thickness, mm
			MProp.Theta_hn=0.;			// hysteresis angle, degrees
			MProp.Theta_hx=0.;			// hysteresis angle, degrees
			MProp.Theta_hy=0.;			// hysteresis angle, degrees
			MProp.NStrands=0;
			MProp.WireD=0;
			MProp.LamFill=1.;			// lamination fill factor;	
			MProp.LamType=0;			// type of lamination;
			MProp.BHpoints=0;
			q[0]=NULL;
		}

		if( _strnicmp(q,"<blockname>",10)==0){
			v=StripKey(s);
			k=strlen(v);
			for(i=0;i<k;i++)
				if(v[i]=='\"'){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -