You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZwSoft.ZwCAD.Runtime;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.EditorInput;
using ZwSoft.ZwCAD.ApplicationServices;
using connor_zwcadm.util;
using connor_zwcadm.commands;
using ZwSoft.ZwCAD.PlottingServices;
using connor_zwcadm.model;
using System.IO;
using Exception = System.Exception;
[assembly: ExtensionApplication(typeof(connor_zwcadm.KMain))]
[assembly: CommandClass(typeof(connor_zwcadm.KMain))]
namespace connor_zwcadm {
class KMain : IExtensionApplication {
public const string PREF_PROP_CONFIG_PREFIX = "Connor_MCAD_";
public const string PREF_UPLOAD_FILE = "Connor_MCAD_UploadFile";
[CommandMethod("Hello")]
public void Hello() {
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nHello World.");
}
[CommandMethod("KReadInfo")]
public void ReadInfo() {
CommandStart("ReadInfo");
try {
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) {
return;
}
AbstractCADCommand command = new ReadInfoCommand(doc.Name);
command.Execute();
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
[CommandMethod("KReadInfo2")]
public void ReadInfo2() {
CommandStart("ReadInfo2");
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) {
return;
}
AbstractCADCommand command = new ReadInfoCommand(doc.Name);
command.Execute();
}
[CommandMethod("KLogin", CommandFlags.Session)]
public void Login() {
CommandStart("Login");
TCUtil.CheckLogin(true);
}
[CommandMethod("KConvertPDF", CommandFlags.Session)]
public void ConvertPDF() {
CommandStart("KConvertPDF");
try {
Dictionary<string, PlotData> plotInfos = new Dictionary<string, PlotData>();
string[] titleBlockNames = ConfigUtil.GetValue("titles").Split(';');
string[] mediaNames = ConfigUtil.GetValue("media_names").Split(';');
string[] rotates = ConfigUtil.GetValue("rotates").Split(';');
int len = KUtil.GetLen(titleBlockNames);
for (int i = 0; i < len; i++) {
PlotData plot = PlotData.Parse(i, titleBlockNames, mediaNames, rotates);
if (plot == null) {
break;
}
KUtil.Log("添加打印信息: " + plot.TitleBlockName + "|" + plot.MediaName + "|" + plot.Rotate);
KUtil.Put(plotInfos, plot.TitleBlockName, plot);
}
if (plotInfos.Count == 0) {
throw new System.Exception("未配置PDF转换数据");
}
Document doc = Application.DocumentManager.MdiActiveDocument;
string path = ConvertPDFCommand.ConvertPDF(doc, plotInfos);
KUtil.Log("PDF: "+path);
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
[CommandMethod("KSaveToTC", CommandFlags.Session)]
public void SaveToTC() {
if (!TCUtil.CheckLogin()) {
return;
}
CommandStart("SaveToTC");
try {
Document doc = Application.DocumentManager.MdiActiveDocument;
AbstractCADCommand command = new SaveCommand(doc.Name, false);
command.Execute();
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
[CommandMethod("KBatchSaveToTC",CommandFlags.Session)]
public void BatchSaveToTC() {
if (!TCUtil.CheckLogin()) {
return;
}
CommandStart("BatchSaveToTC");
try {
// DocumentLock m_lock = Application.DocumentManager.MdiActiveDocument.LockDocument();
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) {
return;
}
KUtil.Log("当前图纸路径:" + doc.Name);
string folderName = System.IO.Path.GetDirectoryName(doc.Name);
string[] filePaths = System.IO.Directory.GetFiles(folderName, "*.dwg");
int fileCnt = KUtil.GetLen(filePaths);
List<string> file_path = new List<string>();
foreach (object doc1 in Application.DocumentManager)
{
if (doc1 is Document)
{
Document openDoc = doc1 as Document;
file_path.Add(openDoc.Name);
}
}
KUtil.Log("当前文件夹下图纸数量:" + fileCnt);
for (int i = 0; i < fileCnt; i++)
{
string filePath = filePaths[i];
KUtil.Log("filePath" + filePath);
if (!file_path.Contains(filePath))
{
OpenFile(filePath);
file_path.Add(filePath);
System.Threading.Thread.Sleep(200);
/* if (!File.Exists(filePath))
{
OpenFile(filePath);
file_path.Add(filePath);
System.Threading.Thread.Sleep(200);
}
else
{
KUtil.Log("图纸不存在");
}*/
}
}
KUtil.Log("打开本地图纸");
AbstractCADCommand command = new SaveCommand(doc.Name, true);
command.Execute();
// m_lock.Dispose();
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
public void OpenFile(string filePath)
{
DocumentCollection acDocMgr = Application.DocumentManager;
Document acNewDoc = acDocMgr.Open(filePath, false); //filepath打开的文件路径false表示文件可改写
Database acDbNewDoc = acNewDoc.Database;
//acDocMgr.MdiActiveDocument = acNewDoc; //将打开的文件设置为当前显示
}
[CommandMethod("KSyncTitleFromTC")]
public void SyncTitleFromTC() {
if (!TCUtil.CheckLogin()) {
return;
}
CommandStart("SyncTitleFromTC");
try {
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) {
return;
}
AbstractCADCommand command = new SyncTitleCommand(doc);
command.Execute();
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
[CommandMethod("KSyncBomViewFromTC")]
public void SyncBomViewFromTC() {
if (!TCUtil.CheckLogin()) {
return;
}
CommandStart("SyncBomViewFromTC");
try {
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) {
return;
}
AbstractCADCommand command = new SyncBomCommand(doc.Name);
command.Execute();
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
[CommandMethod("KSyncBomFromTC")]
public void SyncBomLineFromTC() {
if (!TCUtil.CheckLogin()) {
return;
}
CommandStart("SyncBomLineFromTC");
try {
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) {
return;
}
AbstractCADCommand command = new SyncBomLineCommand(doc.Name);
command.Execute();
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
[CommandMethod("KStartServer", CommandFlags.Session)]
public void StartServer() {
CommandStart("StartServer");
try {
SocketServerCommand command = new SocketServerCommand();
command.ExecuteCommand();
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
[CommandMethod("KStopServer")]
public void StopServer() {
CommandStart("StopServer");
try {
SocketServerCommand.StopServer();
}
catch (System.Exception e) {
KUtil.LogErr(e);
Application.ShowAlertDialog(e.Message);
}
}
public void CommandStart(string msg) {
KUtil.Log("======================= [ Command Start: " + msg + " ] =======================");
}
void IExtensionApplication.Initialize() {
}
void IExtensionApplication.Terminate() {
}
/* public static string Test(Document doc)
{
if (doc == null)
{
throw new Exception("图纸不能为null");
}
string cadPath = doc.Name;
KUtil.Log("转PDF " + cadPath);
string path = cadPath.Substring(0, cadPath.Length - 4) + ".pdf";
KUtil.Log("PDF路径" + path);
Database db = doc.Database;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
KUtil.Log("3");
BlockTable blt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
//if (blt.Has(btlname))
//{
KUtil.Log("4");
BlockTableRecord bltr = tran.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
KUtil.Log("5");
foreach (ObjectId item in bltr)
{
Entity ent = tran.GetObject(item, OpenMode.ForRead) as Entity;
KUtil.Log("6");
if (ent.GetType().Name == "BlockReference")
{
BlockReference bref = (BlockReference)ent;
KUtil.Log("bref.Name " + bref.Name);
if (bref.AttributeCollection.Count != 0)
{
System.Collections.IEnumerator bRefEnum = bref.AttributeCollection.GetEnumerator();
while (bRefEnum.MoveNext())
{
ObjectId aId = (ObjectId)bRefEnum.Current;//这一句极其关键
AttributeReference aRef = (AttributeReference)tran.GetObject(aId, OpenMode.ForRead);
}
}
}
}
//}
tran.Commit();
}
KUtil.Log("转换PDF完成");
return path;
}*/
}
}