声明:部分代码来源于网络,如有疑问,请联系本人删除。
通过C#结合eDrawings API提供接口,实现图纸转换为换.jpg、.tif、.bmp、.stl、.exe、.html、.zip、.edrw、.eprt 和 .eas格式工具,尤其是.html格式,可以实现3D图纸在线查看和旋转、爆炸图等操作。转换服务器或者工作站无需安装SOLIDWORKS。
需要安装软件:
1.eDrawings X64版本,注意安装完注册信息使用公司使用;
2.以及eDrawings X64匹配的SW文档管理器应用程序;
正文:
开发完成的这个应用最终生成一个命令行可执行程序,通过其它语言调用这个工具实现图纸批量转换。
此应用程序可以从命令行运行,并具有以下参数:
- -input:要处理的输入目录或文件路径列表。这些文件可以由 eDrawings 打开(例如 SOLIDWORKS 文件、CATIA、STEP、DXF/DWG 等)。请参阅下面的完整列表:
命令行参数:
- -filter:用于提取输入文件的过滤器(如果 -input 参数包含目录)
- -outdir:导出结果的目录路径。如果不存在该目录,工具将自动创建。如果未指定此参数,则文件将导出到与输入文件相同的文件夹中。
- -format:要将文件导出到的格式列表。支持的格式有:.jpg、.tif、.bmp、.png、.stl、.exe、.htm、.zip、.edrw、.eprt 和 .easm。指定 .e 以将文件导出为 eDrawings 的相应格式(例如,.sldprt 导出为 .eprt,.sldasm 导出为 .easm,.slddrw 导出为 .edrw)。如果未指定此参数,则文件将导出为 eDrawings 格式。
实际示例:
通过命令工具,先进入应用所在目录:
input指示源图纸文件所在路径,format指示导出格式。
主要代码:
EDrawingsHost.cs
using System;
using System.Windows.Forms;
using eDrawings.Interop.EModelViewControl;
namespace Export
{
public class EDrawingsHost : AxHost
{
public event Action<EModelViewControl> ControlLoaded;
private bool m_IsLoaded;
public EDrawingsHost() : base("22945A69-1191-4DCF-9E6F-409BDE94D101")
{
m_IsLoaded = false;
}
protected override void OnCreateControl()
{
base.OnCreateControl();
if (!m_IsLoaded)
{
m_IsLoaded = true;
var ctrl = GetOcx() as EModelViewControl;
ControlLoaded?.Invoke(GetOcx() as EModelViewControl);
}
}
}
}
Program.cs
using eDrawings.Interop.EModelViewControl;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace Export
{
class Program
{
private class ExportData
{
public string InputFilePath { get; private set; }
public string OutputFilePath { get; private set; }
public ExportData(string input, string output)
{
InputFilePath = input;
OutputFilePath = output;
}
}
private const string ARG_INPUT = "-input";
private const string ARG_FILTER = "-filter";
private const string ARG_OUTPUT_DIR = "-outdir";
private const string ARG_FORMAT = "-format";
private const string EDRW_FORMAT = ".e";
private static EModelViewControl m_Ctrl;
private static List<ExportData> m_Job;
private static ExportData m_CurrentExport;
[STAThread]
static void Main(string[] args)
{
try
{
ParseArguments(args);
var eDrwCtrl = new EDrawingsHost();
eDrwCtrl.ControlLoaded += OnEdrawingsControlLoaded;
var winForm = new Form();
winForm.Controls.Add(eDrwCtrl);
eDrwCtrl.Dock = DockStyle.Fill;
winForm.ShowIcon = false;
winForm.ShowInTaskbar = false;
winForm.WindowState = FormWindowState.Minimized;
winForm.ShowDialog();
}
catch (Exception ex)
{
PrintError(ex.Message);
}
}
private static void ParseArguments(string[] args)
{
var inputs = new List<string>();
var filters = new List<string>();
var outDirs = new List<string>();
var formats = new List<string>();
List<string> curList = null;
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals(ARG_INPUT, StringComparison.CurrentCultureIgnoreCase))
{
curList = inputs;
}
else if (args[i].Equals(ARG_FILTER, StringComparison.CurrentCultureIgnoreCase))
{
curList = filters;
}
else if (args[i].Equals(ARG_OUTPUT_DIR, StringComparison.CurrentCultureIgnoreCase))
{
curList = outDirs;
}
else if (args[i].Equals(ARG_FORMAT, StringComparison.CurrentCultureIgnoreCase))
{
curList = formats;
}
else
{
if (curList != null)
{
curList.Add(args[i]);
}
else
{
throw new ArgumentException("Arguments are invalid, specify the correct switch");
}
}
}
foreach (var outDir in outDirs)
{
if (!Directory.Exists(outDir))
{
Directory.CreateDirectory(outDir);
}
}
if (!outDirs.Any())
{
outDirs.Add("");
}
if (!inputs.Any())
{
throw new ArgumentException($"Inputs are not specified. Use {ARG_INPUT} switch to specify the input directory(s) or file(s)");
}
if (!formats.Any())
{
formats.Add(EDRW_FORMAT);
}
var filter = filters.Any() ? filters.First() : "*.*";
var files = new List<string>();
foreach (var input in inputs)
{
if (Directory.Exists(input))
{
files.AddRange(Directory.GetFiles(input, filter, SearchOption.AllDirectories).ToList());
}
else if (File.Exists(input))
{
files.Add(input);
}
else
{
throw new Exception("Specify input file or directory");
}
}
m_Job = new List<ExportData>();
foreach (var file in files)
{
foreach (var outDir in outDirs)
{
foreach (var format in formats)
{
var ext = format;
if (!ext.StartsWith("."))
{
ext = "." + ext;
}
if (ext.Equals(EDRW_FORMAT, StringComparison.CurrentCultureIgnoreCase))
{
switch (Path.GetExtension(file).ToLower())
{
case ".sldprt":
ext = ".eprt";
break;
case ".sldasm":
ext = ".easm";
break;
case ".slddrw":
ext = ".edrw";
break;
default:
throw new ArgumentException($"{EDRW_FORMAT} format is only applicable for SOLIDWORKS files");
}
}
var outFile = Path.Combine(!string.IsNullOrEmpty(outDir) ? outDir : Path.GetDirectoryName(file),
Path.GetFileNameWithoutExtension(file) + ext);
m_Job.Add(new ExportData(file, outFile));
}
}
}
}
public static void OnEdrawingsControlLoaded(EModelViewControl ctrl)
{
Console.WriteLine(string.Format("Starting job. Exporting {0} file(s)", m_Job.Count));
m_Ctrl = ctrl;
m_Ctrl.OnFinishedLoadingDocument += OnDocumentLoaded;
m_Ctrl.OnFailedLoadingDocument += OnDocumentLoadFailed;
m_Ctrl.OnFinishedSavingDocument += OnDocumentSaved;
m_Ctrl.OnFailedSavingDocument += OnSaveFailed;
ExportNext();
}
public static void ExportNext()
{
m_CurrentExport = null;
if (m_Job.Any())
{
m_CurrentExport = m_Job.First();
m_Job.RemoveAt(0);
if (!string.Equals(m_Ctrl.FileName, m_CurrentExport.InputFilePath, StringComparison.CurrentCultureIgnoreCase))
{
m_Ctrl.CloseActiveDoc("");
m_Ctrl.OpenDoc(m_CurrentExport.InputFilePath, false, false, false, "");
}
else
{
ExportCurrentFile();
}
}
else
{
Console.WriteLine("Completed");
Environment.Exit(0);
}
}
public static void OnDocumentLoaded(string fileName)
{
Console.WriteLine(string.Format("Opened {0}", fileName));
ExportCurrentFile();
}
private static void ExportCurrentFile()
{
Console.WriteLine($"Exporting {m_CurrentExport.InputFilePath} to {m_CurrentExport.OutputFilePath}");
m_Ctrl.Save(m_CurrentExport.OutputFilePath, false, "");
}
public static void OnDocumentLoadFailed(string fileName, int errorCode, string errorString)
{
PrintError(string.Format("Failed to load {0}: {1}", fileName, errorString));
ExportNext();
}
private static void OnDocumentSaved()
{
Console.WriteLine("Export completed");
ExportNext();
}
private static void OnSaveFailed(string FileName, int ErrorCode, string ErrorString)
{
PrintError($"Failed to export 'FileName': {ErrorString} [{ErrorCode}]");
ExportNext();
}
public static void PrintError(string msg)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(msg);
Console.ResetColor();
}
}
}
在编译或者运行中报错,可以通过以下手段进行调试:
1.修改VS编译环境为X64;
2.需要.NET Framework4.0以上,如果提示升级直接省升级即可;
3.直接添加dll应用eDrawings.Interop.EModelViewControl.dll,通常位于安装完edrawing的C:\Program Files\Common Files\eDrawings20XX文件夹中。
4.再次编译,生成可执行文件。