- Node.js部分
- 调用C#端供外部调用的dll
- C#部分
- 调用RevitNet.dll,做数据导出exe
- 做外部调用dll
- 总结
由于.Net 的WebSevice在引用RevitNet.dll是出现环境不匹配的问题,这里我们的web服务端是用node.js来做的,通过引用edge.js模块来实现在node.js中调用C#的dll,代码如下:
exportModel: async function(file, params){
try {
let dllPath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModel/ExternalReference/bin/Debug/ExternalReference.dll');
//let dllPath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModelControl/ExportModelControl/bin/Debug/ExportModelControl.dll');
//let exePath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModel/ExportModel/bin/Debug/ExportModel.exe')
let modelSavePath = sails.config.custom.model_sourcefilesPath;
if (!fs.existsSync(modelSavePath)) await fs.mkdirSync(modelSavePath);
let thisUpload = await fileHandleUtil.uploadSync(file, { dirname: modelSavePath });
let modelPath = path.join(sails.config.custom.model_sourcefilesPath, path.basename(thisUpload[0].fd));
var invokedll = null;
// console.log("exe路径:" + exePath);
console.log("模型源文件路径:" + modelPath);
//console.log("导出文件路径:" + exePath);
if (fs.existsSync(dllPath)) {
invokeDll = edge.func({
assemblyFile: dllPath,
typeName: 'ExternalReference.Program',
methodName: 'Export'
})
}else{
assertUtil(null, 'dll path does exist!');
}
var result = null;
if(invokedll!= null){
result = await invokedll(modelPath, true);
}else{
assertUtil(null, 'invokedll miss!');
};
result = result.replace(/rn/g,"");
var arr = result.split(',');
return {
modelId: arr[0],
modelPath: arr[1]
};
} catch (err) {
throw err;
}
}
C#部分
调用RevitNet.dll,做数据导出exe
namespace ExportModel
{
public class Startup
{
static readonly string[] searchs = RevitProductUtility.GetAllInstalledRevitProducts().Select(x => x.InstallLocation).ToArray();
static Startup()
{
AddEnvironmentPaths(searchs);
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
}
[STAThread]
static void Main(string[] args)
{
try
{
Product _product = Product.GetInstalledProduct();
var clientId = new ClientApplicationId(Guid.NewGuid(), "RevitNetTest", "TEST");
_product.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality.");
Autodesk.Revit.ApplicationServices.Application _application = _product.Application;
//string _modelPath = @"H:/Models/TestHouse.rvt";
string _modelPath = args[0];
Document _doc = _application.OpenDocumentFile(_modelPath);
List views = new List(from View3D v in new FilteredElementCollector(_doc).OfClass(typeof(View3D))
where v.CanBePrinted && !v.IsTemplate
select v);
Export export = new Export(_doc, views, "", views[0]);
UploadModel upload = new UploadModel(export.modelPath);
string result = upload.modelId + "," + upload.modelPath;
Console.WriteLine(result);
Environment.Exit(0);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///
/// 向系统中添加环境变量
///
///
static void AddEnvironmentPaths(params string[] paths)
{
try
{
var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
var newPath = string.Join(System.IO.Path.PathSeparator.ToString(), path.Concat(paths));
Environment.SetEnvironmentVariable("PATH", newPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///
/// 重新加载dll
///
///
///
///
private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
try
{
var assemblyName = new AssemblyName(args.Name);
foreach (var item in searchs)
{
var file = string.Format("{0}.dll", System.IO.Path.Combine(item, assemblyName.Name));
if (File.Exists(file))
{
return Assembly.LoadFile(file);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return args.RequestingAssembly;
}
}
}
做外部调用dll
namespace ExternalReference
{
public class Program
{
static Program()
{
}
public async Task
总结
我尝试过把导出方法封装成dll,直接通过node.js或.net WebServer在外部调用。这样做Document _doc = _application.OpenDocumentFile(_modelPath);无法获取到doc,从而无法完成导出。后来只能尝把导出方法封装成一个exe,再通过一个中间的dll去调用exe,获取到了doc。
如果大家有更好的思路或解决方案,欢迎交流!



