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

356 lines
8.2 KiB
C++

// AllDwgDlg.cpp : implementation file
//
#include "stdafx.h"
#include "tapi.h"
//#include "arxtools.h"
#include "teldef.h"
#include "AllDwgDlg.h"
#include "dbsymutl.h"
#include "TGAs.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern "C" HINSTANCE _hdllInstance;
/////////////////////////////////////////////////////////////////////////////
// CAllDwgDlg dialog
void DoTgDwgLibEx(CString csTKFileName)
{
CString csTKName = g_csDwbPath + csTKFileName+_T(".TK");
XString fullFile;
if(!FindFile(csTKName, fullFile)) {
Callme(_T("对不起,没有发现相关上华图库文件!"));
return;
}
CAcModuleResourceOverride vTemp;
CAllDwgDlg dlg(NULL, csTKFileName);
int nDrawMode=DocGetDrawMode();
DocSetDrawMode(DRAW_2D);
if(dlg.DoModal() != IDOK || dlg.m_csInsBlkName == _T("")){
DocSetDrawMode(nDrawMode);
return;
}
DocSetDrawMode(nDrawMode);
XPoint ptIns;
ads_getpoint(NULL, _T("\n请点选插入点 <中心点>:"), ptIns);
ptIns = Ucs2Wcs(ptIns);
InsertBlock((const TCHAR*)dlg.m_csInsBlkName, asPnt3d(ptIns), 1, 1, 0, _T("0"));
Entity ent;
ent.GetLast();
SHads_command(RTSTR,_T("explode"),RTENAME, (TLong_ptr*)ent, RTNONE);
return;
}
//LISP调用 例如:(ITgDwgLib "03d201")
int InvokeTgDwgLib()
{
resbuf *rb=ads_getargs();
RBList rbArgs=rb;
if(rb && rb->restype==RTSTR) {
DoTgDwgLibEx(rb->resval.rstring);
}
ads_retnil();
return RTNORM;
}
//原理图集
void TgDwgLibEx()
{
DoTgDwgLibEx(_T("CIRCUIT"));
}
int GetLibIndex(XString xsRecord, XStringList lstLib)
{
int len = lstLib.Length();
int index = -1;
for(int i=0;i<len;i++){
CString s = (const TCHAR*)lstLib[i];
if(_tcscmp((const TCHAR*)xsRecord, (const TCHAR*)lstLib[i]) == 0){
index = i;
return index;
}
}
return index;
}
CAllDwgDlg::CAllDwgDlg(CWnd* pParent /*=NULL*/, CString csTKName)
: CDialog(CAllDwgDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAllDwgDlg)
m_csText = _T("");
m_iComSel = -1;
//}}AFX_DATA_INIT
mPreviewDb = NULL;
m_csInsBlkName = _T("");
m_csTmpDwg = _T("");
m_csTKName = csTKName;
}
CAllDwgDlg::~CAllDwgDlg()
{
if(m_csTmpDwg != _T(""))
_tremove(m_csTmpDwg);
if(!mPreviewDb){
delete mPreviewDb;
mPreviewDb = NULL;
}
}
void CAllDwgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAllDwgDlg)
DDX_Control(pDX, IDC_COMBO_TYPE, m_comboType);
DDX_Control(pDX, IDC_LIST_NAME, m_listName);
DDX_Text(pDX, IDC_STATIC_TEXT, m_csText);
DDX_CBIndex(pDX, IDC_COMBO_TYPE, m_iComSel);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAllDwgDlg, CDialog)
//{{AFX_MSG_MAP(CAllDwgDlg)
ON_LBN_SELCHANGE(IDC_LIST_NAME, OnSelchangeListName)
ON_WM_PAINT()
ON_CBN_SELCHANGE(IDC_COMBO_TYPE, OnSelchangeComboType)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAllDwgDlg message handlers
BOOL CAllDwgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// DocSetDrawMode(DRAW_2D);
mPreviewCtrl.SetOrbitFlag(OBF_ZOOM|OBF_PAN|OBF_ORBIT);
if (!mPreviewCtrl.SubclassDlgItem(IDC_STATIC_DWG, this))
return FALSE;
ResetCombox();
m_iComSel = 0;
UpdateData(FALSE);
OnSelchangeComboType();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CAllDwgDlg::ResetCombox()
{
CStringList lstUType;
POSITION pos;
CString csTKUAllName = g_csDwbPath + m_csTKName + _T(".TK");
if(GetTKType(csTKUAllName, lstUType) == FALSE)
return;
pos = lstUType.GetHeadPosition();
BOOL bIsTKNew = FALSE;
while(pos != NULL)
{
CString csAllType=lstUType.GetNext(pos);
if(csAllType == _T("电机主回路"))
continue;
m_comboType.AddString(csAllType);
}
}
void CAllDwgDlg::OnOK()
{
if(m_csCurRecord == _T("")){
CDialog::OnOK();
return;
}
XString xsBlkName;
CString csDwbName;
csDwbName = g_csDwbPath + m_csTKName + _T(".DWB");
if(LoadDwbBlock(csDwbName, m_csCurRecord, xsBlkName) != RTNORM) {
MessageBox(_T("加载图块失败!"));
}
m_csInsBlkName = xsBlkName;
CDialog::OnOK();
}
void CAllDwgDlg::OnButtonShow()
{
if(m_csTmpDwg != _T(""))
_tremove(m_csTmpDwg);
TCHAR tempPrefix[256], dwgPrefix[256];
GetTempPath(255, tempPrefix);
CString csDwbName, csDwgName;
csDwbName = g_csDwbPath + m_csTKName + _T(".DWB");
XDrawingLib DwbLib(csDwbName);
XStringList lstDwbLib;
DwbLib.GetAll(lstDwbLib);
int index = GetLibIndex((const TCHAR*)m_csCurRecord, lstDwbLib);
_tcscpy(dwgPrefix, tempPrefix);
_tcscat(dwgPrefix, (const TCHAR*)m_csCurRecord);
_tcscat(dwgPrefix, _T(".dwg"));
if(index > -1)
DwbLib.Copy(index, dwgPrefix);
m_csTmpDwg = dwgPrefix;
mPreviewCtrl.clearAll();
if(!mPreviewDb){
mPreviewDb = new AcDbDatabase(false,true);
}
else{
delete mPreviewDb;
mPreviewDb = new AcDbDatabase(false,true);
}
#if ADS > 20
mPreviewDb->readDwgFile((const TCHAR*)m_csTmpDwg, AcDbDatabase::OpenMode::kForReadAndAllShare, false);
#else
mPreviewDb->readDwgFile((const TCHAR*)m_csTmpDwg, _SH_DENYNO, false);
#endif
// modify by cq 2017-01-05 支持CAD2017
UpdatePreview();
}
void CAllDwgDlg::OnSelchangeListName()
{
int iSel = m_listName.GetCurSel();
m_listName.GetCurSel();
if(iSel < 0)
return;
Redraw();
OnButtonShow();
}
void CAllDwgDlg::Redraw()
{
UpdateData();
CString csName, csItem, csRecord;
int iSel = m_listName.GetCurSel();
m_listName.GetCurSel();
if(iSel < 0)
return;
m_listName.GetText(iSel, csItem);
int len = m_lstAllDwg.Length();
for(int i=0;i<len;i++){
CString csTmp = m_lstAllDwg[i]->m_csDesName;
if(csItem == csTmp){
csRecord = m_lstAllDwg[i]->m_csRecord;
break;
}
}
m_csCurRecord = csRecord;
m_csText = csItem;
UpdateData(FALSE);
}
void CAllDwgDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
}
//计算AcDbDatabase的外包围盒
void CAllDwgDlg::GetDwgdatabaseExt(XPoint &ptMax, XPoint &ptMin)
{
DList<AcDbObjectId> ssAllPipe;
if(GetEntityFromDatabase(ssAllPipe, *mPreviewDb, _T("LINE,INSERT,TEXT"), 1, ACDB_MODEL_SPACE) != Acad::eOk)
return;
AcDbExtents extAll;
int len = ssAllPipe.Length();
for(int i=0;i<len;i++){
AcDbObjectId id = *ssAllPipe[i];
OPENOBJ_BEGIN(id, AcDb::kForRead, AcDbEntity, pEnt);
if(pEnt){
AcDbExtents ext;
pEnt->getGeomExtents(ext);
extAll.addExt(ext);
}
OPENOBJ_END();
}
ptMax = extAll.maxPoint();
ptMin = extAll.minPoint();
}
void CAllDwgDlg::UpdatePreview()
{
mPreviewCtrl.init(true);
{
XPoint ptMax, ptMin;
GetDwgdatabaseExt(ptMax, ptMin);
XPoint ptCen;
double nWidth(0),nHeight(0);
XLine line(ptMax, ptMin);
line.MidPoint(ptCen);
nWidth = ads_distance(ptMax, ptMin);
nHeight = nWidth;
//double nHeight = line.GetLength();
AcGePoint3d target = ptCen;//(0.0,0.0,0.0);
AcGePoint3d position(target);
position.z += 1;
AcGsView* pView = mPreviewCtrl.view();
pView->setView(position, target,
AcGeVector3d(0.0, 1.0, 0.0), // upvector
nWidth, nHeight);
AcDbObjectId idMSpace = acdbSymUtil()->blockModelSpaceId(mPreviewDb);
OPENOBJ_BEGIN(idMSpace, AcDb::kForRead, AcDbBlockTableRecord, pBlockTableRecord);
if(pBlockTableRecord)
mPreviewCtrl.view()->add(pBlockTableRecord, mPreviewCtrl.model());
OPENOBJ_END();
}
mPreviewCtrl.Invalidate();
}
BOOL CAllDwgDlg::DestroyWindow()
{
mPreviewCtrl.clearAll();
return CDialog::DestroyWindow();
}
void CAllDwgDlg::OnSelchangeComboType()
{
UpdateData();
CString csType;
m_comboType.GetLBText(m_iComSel, csType);
CString csTKName = g_csDwbPath + m_csTKName + _T(".TK");
CodeBase db;
XDbfTable *pTable = db.Open(csTKName);
m_lstAllDwg.Reset();
m_listName.ResetContent();
if(!pTable) {
CString csTmp;
csTmp.Format(_T("%s文件只读或损坏!"), csTKName);
AfxMessageBox(csTmp);
// AfxMessageBox(_T("TK文件只读或损坏!"));
return;
}
for(pTable->MoveFirst();!pTable->IsEOF();pTable->MoveNext()) {
CString sKind, sItem, sDrawing;
GET_FIELD(pTable, _T("kind"), sKind);
GET_FIELD(pTable, _T("item"), sItem);
GET_FIELD(pTable, _T("drawing"), sDrawing);
if(sKind == csType && sDrawing != _T("")) {
CBlockStruct *pData = new CBlockStruct();
pData->m_csTKName = csTKName;
pData->m_csRecord = (const TCHAR*)sDrawing;
pData->m_csDesName = (const TCHAR*)sItem;
m_lstAllDwg.Append(pData);
m_listName.AddString((const TCHAR*)sItem);
}
}
}