Files
envi-code/SourceCode/Code2026/tg_shs/shs_public/TgBlkLibFun.cpp
T
2026-09-28 15:28:50 +08:00

620 lines
17 KiB
C++

#include "stdafx.h"
//#include <arxtools.h>
#include "tapi.h"
#include "XFilePathName.h"
#include "Block.h"
BOOL CreateTKFile(const TCHAR *pszFile)
{
FieldInfo fields[13];
fields[0] = FieldInfo(_T("ConType"), _T('C'), 32);
/*导体类型 0:钢芯铝绞线 (架空线)
1:铝绞线 (架空线)
2:聚氯乙烯绝缘聚氯乙烯护套铜芯 (电缆)
3:聚氯乙烯绝缘聚氯乙烯护套铝芯 (电缆)
4:聚氯乙烯绝缘内钢带铠装聚氯乙烯护套铜芯 (电缆)
5:聚氯乙烯绝缘内钢带铠装聚氯乙烯护套铝芯 (电缆)
*/
fields[1] = FieldInfo(_T("MainSec"), _T('N'), 32, 6);
// 电缆主线芯截面(mm*mm) (电缆)
// 导体截面(mm*mm) (架空线)
fields[2] = FieldInfo(_T("NeutralSec"), _T('N'), 32, 6);
// 电缆中性线芯截面(mm*mm)(电缆)
fields[3] = FieldInfo(_T("ResistAl"), _T('N'), 32, 6);
// 铝芯电缆电阻(欧/公里)(电缆)
fields[4] = FieldInfo(_T("ResistCu"), _T('N'), 32, 6);
// 铜芯电缆电阻(欧/公里)(电缆)
fields[5] = FieldInfo(_T("Reactance"), _T('N'), 32, 6);
// 电缆电抗(欧/公里) (电缆)
fields[6] = FieldInfo(_T("Capacity15"), _T('N'), 32, 6);
// 导体载流量(A) 20
fields[7] = FieldInfo(_T("Capacity20"), _T('N'), 32, 6);
// 导体载流量(A) 20
fields[8] = FieldInfo(_T("Capacity25"), _T('N'), 32, 6);
// 导体载流量(A)
fields[9] = FieldInfo(_T("Capacity30"), _T('N'), 32, 6);
// 导体载流量(A)
fields[10] = FieldInfo(_T("Capacity35"), _T('N'), 32, 6);
// 导体载流量(A)
fields[11] = FieldInfo(_T("Capacity40"), _T('N'), 32, 6);
// 导体载流量(A)
CodeBase db1;
// #ifndef _WIN64
// XDbfTable *pTable1 = db1.Create(pszFile, fields);
// #else
XDbfTable *pTable1 = db1.Create(pszFile, fields, 4);
// #endif
if(!pTable1)
return FALSE;
CString type[] = {_T("VV_1"), _T("VLV_1"), _T("VV22_1"), _T("VLV22_1")};
double nMainSec,nNeutralSec, nZll20, nZll25, nZll30, nZll35, nZll40, nResistAl, nResistCu,nReactance;
CodeBase db;
db.read_only =1;
int nModel;
XDbfTable *pTable = db.Open(_T("d:\\TELCALC.DBF"));
if(!pTable) return 0;
pTable->MoveFirst();
while(!pTable->IsEOF()) {
GET_FIELD(pTable, _T("ConModel"), nModel);
GET_FIELD(pTable, _T("MainSec"), nMainSec);
GET_FIELD(pTable, _T("NeutralSec"), nNeutralSec);
GET_FIELD(pTable, _T("Capacity20"), nZll20);
GET_FIELD(pTable, _T("Capacity25"), nZll25);
GET_FIELD(pTable, _T("Capacity30"), nZll30);
GET_FIELD(pTable, _T("Capacity35"), nZll35);
GET_FIELD(pTable, _T("Capacity40"), nZll40);
GET_FIELD(pTable, _T("ResistAl"), nResistAl);
GET_FIELD(pTable, _T("ResistCu"), nResistCu);
GET_FIELD(pTable, _T("Reactance"), nReactance);
if(nModel>1){
pTable1->AppendBegin();
SET_FIELD(pTable1, _T("ConType"), (const TCHAR*)type[nModel-2]);
SET_FIELD(pTable1, _T("MainSec"), nMainSec);
SET_FIELD(pTable1, _T("NeutralSec"), nNeutralSec);
SET_FIELD(pTable1, _T("ResistAl"), nResistAl);
SET_FIELD(pTable1, _T("ResistCu"), nResistCu);
SET_FIELD(pTable1, _T("Capacity15"), -1);
SET_FIELD(pTable1, _T("Capacity20"), nZll20);
SET_FIELD(pTable1, _T("Capacity25"), nZll25);
SET_FIELD(pTable1, _T("Capacity30"), nZll30);
SET_FIELD(pTable1, _T("Capacity35"), nZll35);
SET_FIELD(pTable1, _T("Capacity40"), nZll40);
SET_FIELD(pTable1, _T("Reactance"), nReactance);
pTable1->AppendEnd();
}
pTable->MoveNext();
}
BOOL bRet = (pTable!=NULL);
pTable->Close();
return bRet;
}
void TgMakeSlide()
{
TCHAR dwgPrefix[256]=_T("");
GetTempPath(255, dwgPrefix);
if(dwgPrefix[_tcslen(dwgPrefix)-1]!=_T('\\'))
_tcscat(dwgPrefix, _T("\\"));
XString xsFullSld;
xsFullSld << dwgPrefix << _T("_ELECBLK") << _T(".sld");
///////////////////////////////////////////////////////////////////////////////////
ZOOME;
ads_command(RTSTR, _T("REGEN"), RTNONE);
ads_command(RTSTR, _T("MSLIDE"), RTSTR, (TCHAR *)xsFullSld, RTNONE);
ZOOMP;
}
//是否全部为数字
//BOOL IsDigital(const TCHAR *pStr)
//{
// ASSERT(pStr);
// int nLen=_tcslen(pStr);
// if(!nLen)
// return FALSE;
// for(int i=0; i<nLen; i++) {
// if(!_istdigit(pStr[i]))
// return FALSE;
// }
// return TRUE;
//}
int GetNextDrawing(const TCHAR *tkName, TCHAR *dwg)
{
CodeBase db;
XDbfTable *pTable = db.Open(tkName);
if(!pTable)
{
CString csTmp;
csTmp.Format(_T("%s文件只读或损坏!"), tkName);
AfxMessageBox(csTmp);
// AfxMessageBox(_T("TK文件只读或损坏!"));
return RTERROR;
}
Array<CString> recs;
CString str;
CString sTemp;
for (pTable->MoveFirst(0);!pTable->IsEOF();pTable->MoveNext(0))
{
GET_FIELD(pTable,_T("Drawing"), sTemp);
str = sTemp;
if (IsDigital(str)&&_tcslen(str)==8)
recs.AddTail(LPCTSTR(str));
}
if (recs.GetLength() >= 1)
{
CString maxStr = recs[0];
int i = 1;
while (i < recs.GetLength())
{
if (maxStr<recs[i])
maxStr = recs[i];
i++;
}
if(maxStr && IsDigital(maxStr))
{
int slen = (int)_tcslen(maxStr);
TCHAR a1=maxStr[slen-1], a2=maxStr[slen-2], a3=maxStr[slen-3],a4 = maxStr[slen-4],a5 = maxStr[slen-5],a6 = maxStr[slen-6];
if(a1>=_T('0') && a1<_T('9')) a1++;
else if(a1==_T('9') && a2>=_T('0') && a2<_T('9'))
{ a1=_T('0'); a2++; }
else if(a1==_T('9') && a2==_T('9') && a3>=_T('0') && a3<_T('9'))
{ a1=a2=_T('0'); a3++; }
else if(a1==_T('9') && a2==_T('9') && a3==_T('9') && a4>=_T('0') &&a4<_T('9'))
{ a1=a2=a3=_T('0'); a4++; }
else if(a1==_T('9') && a2==_T('9') && a3==_T('9') && a4==_T('9') && a5>=_T('0')&&a5<_T('9'))
{ a1=a2=a3=a4=_T('0'); a5++; }
else if(a1==_T('9') && a2==_T('9') && a3==_T('9') && a4==_T('9') && a5==_T('9')&&a6>=_T('0')&&a6<_T('9'))
{a1=a2=a3=a4=a5=_T('0'); a6++; }
maxStr.Delete(slen-6,6);
maxStr = maxStr + a6;
maxStr = maxStr + a5;
maxStr = maxStr + a4;
maxStr = maxStr + a3;
maxStr = maxStr + a2;
maxStr = maxStr + a1;
_tcscpy(dwg, maxStr);
}
}
else
{
_tcscpy(dwg, _T("00000001"));
return RTNORM;
}
return RTNORM;
}
CString AddNewDwg2Dwb(const TCHAR* szTKName,
const TCHAR* szDwgName,
const TCHAR* szSldName,
const TCHAR* szType,
const TCHAR* szDesName)
{
CString csTKName = szTKName;
CString csDwgName = szDwgName;
CString csSldName = szSldName;
CString csType = szType;
CString csDesName = szDesName;
int len = csTKName.GetLength();
CString csDwbFileName = csTKName.Left(len - 2) + _T("dwb");
CString csSlbFileName = csTKName.Left(len - 2) + _T("slb");
XString xsRetStr;
CString csErr = _T("");
CodeBase db;
XDbfTable *pTable = db.Open(csTKName);
if(!pTable) {
CString csTmp;
csTmp.Format(_T("%s文件只读或损坏!"), csTKName);
AfxMessageBox(csTmp);
// AfxMessageBox(_T("TK文件只读或损坏!"));
return csErr;
}
if(GetNextDrawing((const TCHAR*)csTKName, xsRetStr)!=RTNORM) {
AfxMessageBox(_T("无法向DWB中增加记录"));
return csErr;
}
XString xsNewDwgName, xsNewSldName;
XFilePathName xfpn = csDwgName;
xsNewDwgName << (const TCHAR*)xfpn.getDriver()
<< (const TCHAR*)xfpn.getDir() << (const TCHAR*)xsRetStr << _T(".dwg");
xsNewSldName << (const TCHAR*)xfpn.getDriver()
<< (const TCHAR*)xfpn.getDir() << (const TCHAR*)xsRetStr << _T(".sld");
// xsNewDwgName << (const TCHAR*)xfpn.getDriver()
// << (const TCHAR*)xfpn.getDir() << _T("\\") << (const TCHAR*)xsRetStr << _T(".dwg");
// xsNewSldName << (const TCHAR*)xfpn.getDriver()
// << (const TCHAR*)xfpn.getDir() << _T("\\") << (const TCHAR*)xsRetStr << _T(".sld");
XSlideLib slbMan(csSlbFileName);
if(!slbMan) {
AfxMessageBox(_T("无法打开Slb文件"));
return csErr;
_tremove(csSldName);
}
int ret;
_trename(csSldName,xsNewSldName);
ret = slbMan.Append(xsNewSldName);
slbMan.Close();
_tremove(xsNewSldName);
XDrawingLib dwbMan(csDwbFileName);
if(!dwbMan) {
AfxMessageBox(_T("无法打开DWB文件"));
return csErr;
_tremove(csDwgName);
}
_trename(csDwgName,xsNewDwgName);
ret = dwbMan.Append(xsNewDwgName);
dwbMan.Close();
_tremove(xsNewDwgName);
pTable->AppendBegin();
SET_FIELD(pTable, _T("kind"), (const TCHAR*)csType);
SET_FIELD(pTable, _T("item"), (const TCHAR*)csDesName);
SET_FIELD(pTable, _T("Drawing"), (const TCHAR*)xsRetStr);
pTable->AppendEnd();
pTable->Close(); //sr add 11.15
CString csRet = (const TCHAR*)xsRetStr;
return csRet;
}
CString RebuildDwgInDwb(const TCHAR* szTKName,
const TCHAR* szDwgName,
const TCHAR* szSldName,
const TCHAR* szDesName,
const TCHAR* szRecord)
{
CString csTKName = szTKName;
CString csDwgName = szDwgName;
CString csSldName = szSldName;
CString csDesName = szDesName;
CString csRecord = szRecord;
int len = csTKName.GetLength();
CString csDwbFileName = csTKName.Left(len - 2) + _T("dwb");
CString csSlbFileName = csTKName.Left(len - 2) + _T("slb");
XString xsRetStr;
CString csErr = _T("");
CodeBase db;
XDbfTable *pTable = db.Open(csTKName);
if(!pTable) {
CString csTmp;
csTmp.Format(_T("%s文件只读或损坏!"), csTKName);
AfxMessageBox(csTmp);
// AfxMessageBox(_T("TK文件只读或损坏!"));
return csErr;
}
if(GetNextDrawing((const TCHAR*)csTKName, xsRetStr)!=RTNORM) {
AfxMessageBox(_T("无法想DWB中增加记录"));
return csErr;
}
XString xsNewDwgName, xsNewSldName;
XFilePathName xfpn = csDwgName;
xsNewDwgName << (const TCHAR*)xfpn.getDriver()
<< (const TCHAR*)xfpn.getDir() << _T("\\") << (const TCHAR*)xsRetStr << _T(".dwg");
xsNewSldName << (const TCHAR*)xfpn.getDriver()
<< (const TCHAR*)xfpn.getDir() << _T("\\") << (const TCHAR*)xsRetStr << _T(".sld");
XSlideLib slbMan(csSlbFileName);
if(!slbMan) {
AfxMessageBox(_T("无法打开Slb文件"));
return csErr;
_tremove(csSldName);
}
int ret;
_trename(csSldName,xsNewSldName);
ret = slbMan.Append(xsNewSldName);
slbMan.Close();
_tremove(xsNewSldName);
XDrawingLib dwbMan(csDwbFileName);
if(!dwbMan) {
AfxMessageBox(_T("无法打开DWB文件"));
return csErr;
_tremove(csDwgName);
}
_trename(csDwgName,xsNewDwgName);
ret = dwbMan.Append(xsNewDwgName);
dwbMan.Close();
_tremove(xsNewDwgName);
for(pTable->MoveFirst();!pTable->IsEOF();pTable->MoveNext()){
CString sDrawing;
GET_FIELD(pTable, _T("drawing"), sDrawing);
if(_tcscmp(sDrawing, csRecord) == 0) {
SET_FIELD(pTable, _T("drawing"), (const TCHAR*)xsRetStr);
if(csDesName != _T(""))
SET_FIELD(pTable, _T("item"), (const TCHAR*)csDesName);
break;
}
}
CString csRet = (const TCHAR*)xsRetStr;
return csRet;
}
BOOL GetTKType(const TCHAR* szFileName, CStringList &lstType)
{
CodeBase db;
db.read_only =1;
XDbfTable *pTable = db.Open(szFileName);
if(!pTable) return FALSE;
CString sKind;
CBlockStruct Blk;
pTable->MoveFirst();
while(!pTable->IsEOF()) {
GET_FIELD(pTable, _T("kind"), sKind);
if(sKind != _T("")){
POSITION pos;
pos = lstType.Find((const TCHAR*)sKind);
if(pos == NULL){
pos = lstType.GetTailPosition();
pos = lstType.InsertAfter(pos, (const TCHAR*)sKind); //在链表尾插入 块 结构
}
}
pTable->MoveNext();
}
pTable->Close(); //sr add 11.15
return TRUE;
}
BOOL GetTypeRecord(const TCHAR* szFileName,
const TCHAR* szBlkType,
CList<CBlockStruct, CBlockStruct&> &lstBlock)
{
CString csBlkType = szBlkType;
CodeBase db;
db.read_only = 1;
XDbfTable *pTable = db.Open(szFileName);
if(!pTable) return FALSE;
CString sKind, sItem, sDrawing;
CBlockStruct Blk;
pTable->MoveFirst();
while(!pTable->IsEOF()) {
GET_FIELD(pTable, _T("kind"), sKind);
GET_FIELD(pTable, _T("item"), sItem);
GET_FIELD(pTable, _T("Drawing"), sDrawing);
if((sKind == (const TCHAR *)csBlkType) && (sDrawing != _T(""))) {
Blk.m_csDesName = sItem;
Blk.m_csRecord = sDrawing;
POSITION pos1;
pos1 = lstBlock.GetTailPosition();
pos1 = lstBlock.InsertAfter(pos1, Blk); //在链表尾插入 块 结构
}
pTable->MoveNext();
}
pTable->Close(); //sr add 11.15
return TRUE;
}
BOOL IsTgBlkExist(const TCHAR* szBlkName)
{
CString csTk, csRecord;
CString sDrawing;
SplitBlkStr(szBlkName, csTk, csRecord);
CodeBase db;
db.read_only = 1;
CString csTmpTKName = g_sDwbPath + csTk + _T(".tk");
XDbfTable *pTable = db.Open(csTmpTKName);
if(!pTable)
return FALSE;
for(pTable->MoveFirst();!pTable->IsEOF();pTable->MoveNext()){
GET_FIELD(pTable, _T("Drawing"), sDrawing);
if(sDrawing == (const TCHAR *)csRecord){
return TRUE;
}
}
return FALSE;
}
CString GetTgBlkTypeInTk(const TCHAR* szBlkName)
{
CString csTk, csRecord, csRet;
CString sKind, sDrawing;
SplitBlkStr(szBlkName, csTk, csRecord);
CodeBase db;
db.read_only = 1;
CString csTmpTKName = g_sDwbPath + csTk + _T(".tk");
XDbfTable *pTable = db.Open(csTmpTKName);
if(!pTable)
return csRet;
for(pTable->MoveFirst();!pTable->IsEOF();pTable->MoveNext()){
GET_FIELD(pTable, _T("Drawing"), sDrawing);
GET_FIELD(pTable, _T("kind"), sKind);
if(sDrawing == (const TCHAR *)csRecord){
csRet = (const TCHAR *)sKind;
return csRet;
}
}
return csRet;
}
BOOL GetTypeAllRecord(const TCHAR* szFileName,
const TCHAR* szBlkType,
CList<CBlockStruct, CBlockStruct&> &lstBlock,
BOOL bOnlyCurType)
{
CString csFileName = szFileName;
CString csBlkType = szBlkType;
CBlockStruct Blk;
if(!bOnlyCurType && csFileName == _T("equip")){ //读高频设备
int iLastBlkMaxNum = 6;
XStringList lstLastEq;
CRegProperty regProp2(GetRegLocation(), _T("TElecConfig"));
regProp2.SetTopRootKey(HKEY_CURRENT_USER);
regProp2.GetValue(_T("LastBlkMaxNum"), iLastBlkMaxNum);
if(iLastBlkMaxNum < 0)
iLastBlkMaxNum = 6;
CRegProperty regProp(GetRegLocation(), _T("LastInsEquip"));
regProp.SetTopRootKey(HKEY_CURRENT_USER);
for(int i=0;i<iLastBlkMaxNum;i++) {
XString sKey(256);
CString csValue;
sKey << _T("The recent InsBlk") << (i + 1);
regProp.GetValue(sKey, csValue);
if(csValue.IsEmpty() || csValue.GetLength() > 255) continue;
lstLastEq.Append(tstrnew(csValue));
}
for(int j=0;j<lstLastEq.Length();j++){
CString csTmp = lstLastEq[j];
if(!IsTgBlkExist((const TCHAR*)csTmp))
continue;
CString csTk, csRecord;
SplitBlkStr(csTmp, csTk, csRecord);
CString csTmpTKName = g_sDwbPath + csTk + _T(".tk");
if((csTk != _T("")) && (csRecord != _T(""))) {
Blk.m_csTKName = csTmpTKName;
Blk.m_csDesName = _T("*最近使用图块*");
Blk.m_csRecord = csRecord;
lstBlock.InsertAfter(lstBlock.GetTailPosition(), Blk); //在链表尾插入 块 结构
}
}
}
CodeBase db;
db.read_only = 1;
CString sKind, sItem, sDrawing;
CString csUserTKName = g_sDwbPath + csFileName + _T("_U.tk");
if(FindFile(csUserTKName)){
XDbfTable *pTable2 = db.Open(csUserTKName);
if(!pTable2) return FALSE;
pTable2->MoveFirst();
while(!pTable2->IsEOF()) {
GET_FIELD(pTable2, _T("kind"), sKind);
GET_FIELD(pTable2, _T("item"), sItem);
GET_FIELD(pTable2, _T("Drawing"), sDrawing);
if((sKind == (const TCHAR *)csBlkType) && (sDrawing != _T(""))) {
Blk.m_csTKName = csUserTKName;
Blk.m_csDesName = sItem;
Blk.m_csRecord = sDrawing;
lstBlock.InsertAfter(lstBlock.GetTailPosition(), Blk); //在链表尾插入 块 结构
}
pTable2->MoveNext();
}
}
CString csSysTKName = g_sDwbPath + csFileName + _T(".tk");
XDbfTable *pTable = db.Open(csSysTKName);
if(!pTable) return FALSE;
pTable->MoveFirst();
while(!pTable->IsEOF()) {
GET_FIELD(pTable, _T("kind"), sKind);
GET_FIELD(pTable, _T("item"), sItem);
GET_FIELD(pTable, _T("Drawing"), sDrawing);
if((sKind == (const TCHAR *)csBlkType) && (sDrawing != _T(""))) {
Blk.m_csTKName = csSysTKName;
Blk.m_csDesName = sItem;
Blk.m_csRecord = sDrawing;
lstBlock.InsertAfter(lstBlock.GetTailPosition(), Blk); //在链表尾插入 块 结构
}
pTable->MoveNext();
}
return TRUE;
}
CString GetBlkDesName(const TCHAR* szRecord,const TCHAR* szTKName)
{
CString csName;
CodeBase db;
db.read_only = 1;
XDbfTable *pTable = db.Open(szTKName);
if(!pTable) {
CString csTmp;
csTmp.Format(_T("%s无法打开!"), szTKName);
adsout << (const TCHAR *)csTmp;
// AfxMessageBox(_T("TK文件只读或损坏!"));
return CString();
}
for(pTable->MoveFirst();!pTable->IsEOF();pTable->MoveNext()){
CString sDrawing, sItem;
GET_FIELD(pTable, _T("drawing"), sDrawing);
GET_FIELD(pTable, _T("item"), sItem);
if(_tcscmp(sDrawing, szRecord) == 0) {
csName = (const TCHAR*)sItem;
return csName;
}
}
return CString();
}
AcDbObjectId InsertBlock(const TCHAR*szBlkName, AcGePoint3d pt, double XScale, double YScale, double Rotation, const TCHAR* szLayer)
{
AcDbObjectId retId;
CBlock blk(szBlkName, pt, XScale, Rotation, szLayer);
blk.Insert(TRUE);
Entity ent;
ent.GetLast();
acdbGetObjectId(retId, ent);
return retId;
}
BOOL InsertBlock(const TCHAR*szBlkName, XPoint pt, double nScale, double Rotation, const TCHAR* szLayer)
{
InitBlock(szBlkName);
AcDbBlockTable *pBlockTable;
AcDbBlockTableRecord* pRecord;
acdbCurDwg()->getBlockTable(pBlockTable, AcDb::kForRead);
if ((pBlockTable->getAt(szBlkName, pRecord, AcDb::kForRead)) != Acad::eOk)
return FALSE;
pBlockTable->close();
AcDbObjectId BlockId;
BlockId = pRecord->objectId();
pRecord->close();
AcDbBlockReference *pBlkRef = new AcDbBlockReference;
pBlkRef->setBlockTableRecord(BlockId);
pBlkRef->setRotation(Rotation);
pBlkRef->setPosition(AcGePoint3d(pt));
pBlkRef->setScaleFactors(nScale);
pBlkRef->setLayer(szLayer);
PostToDb(pBlkRef);
pBlkRef->close();
return TRUE;
}