AVEVA PML.Net for DWG
Abstract. AVEVA PmlNet allows you to instantiate and invoke methods on .NET objects from PML proxy objects. The PML proxy objects behave just like any other PML object. By PML proxy object you can use many other C# resources easily. The paper show an example to use PML proxy object to read and write AutoCAD DXF/DWG files by the OpenDWG library in AVEVA PDMS.
Key Words. AVEVA, PDMS, Marine, Plant, OpenDWG, AutoCAD, DWG, DXF
1. Introduction
使用AVEVA PML.Net可以编写代理PML对象,这种代理对象可以在PML中被调用,其调用方式和其他PML对象完全一样。且PML.Net编写的dll部署简单,不像AddIn开发出的插件那样需要比较复杂的配置。只需要将生成的dll放到PDMS安装目录,即可以在PML中调用了。
使用PML.Net编写的PML代理类可以大大方便C#开发者的开发,且可使用C#相关的大量资源,甚至是C++等资源。如本文所说的OpenDWG库,可以实现在PDMS中直接对DWG/DXF文件进行读写。
做CAD开发的应该都听说过AutoCAD,而读写DWG文件的库,除了Autodesk公司的以外,还有一个组织开发出来的OpenDWG,这个组织的名字是Open Design Alliance(ODA)。因为DWG文件广泛使用且没有公布文件格式,所以才有这个ODA的存在。在AVEVA 12.0版本中无意发现了DWGDirect相关的库,如图所示:
Figure 1.1 DWGDirect_Net by Open Design Alliance
本文对如何使用PML.Net编写代理PML类起到抛砖引玉的作用,读者可以在此基础上尽情自由发挥,编写出更加实用的程序。
2.OpenDWG usage
既然在官方的版本中提供了DWGDirect_Net库,那么就可以正大光明地用在自己的程序开发中来对DWG文件做些处理,如为Draft的出图使用DWG模板、修改导出的DXF图纸中的中文字体、将DXF文件直接转换成DWG格式等等。
DWGdirect_Net的用法很简单,下面给出一个最简单的例子,更详细的内容可参考ODA提供的示例程序。
using System.Text;
using System.Collections.Generic;
using DWGdirect.Runtime;
using DWGdirect.Geometry;
using DWGdirect.DatabaseServices;
namespace OpenDwgTest
{
/// <summary>
/// a simple demo about how to use OpenDWG.
/// <author> Shing Liu([email protected]) </author>
/// </summary>
class Program
{
static void Main( string [] args)
{
// initialize DWGdirect.
using (Services aService = new Services())
{
try
{
using (Database aDatabase = new Database())
{
using (BlockTable aBlockTable = (BlockTable)aDatabase.BlockTableId.GetObject(OpenMode.ForRead))
{
ObjectId aModelSpaceId = aBlockTable[BlockTableRecord.ModelSpace];
using (BlockTableRecord aRecord = (BlockTableRecord)aModelSpaceId.GetObject(OpenMode.ForWrite))
{
// make a cirlce.
Circle aCircle = new Circle();
aCircle.SetDatabaseDefaults(aDatabase);
aCircle.Radius = 50.0 ;
aRecord.AppendEntity(aCircle);
// make a text.
DBText aText = new DBText();
aText.SetDatabaseDefaults(aDatabase);
aText.Position = new Point3d( - 35.0 , 10.0 , 0.0 );
aText.TextString = " Hello World! by OpenDWG " ;
aText.Height = 5.0 ;
aText.WidthFactor = 0.7 ;
aRecord.AppendEntity(aText);
}
}
aDatabase.SaveAs( " d:/test.dwg " , DwgVersion.vAC18);
}
}
catch (System.Exception e)
{
Console.WriteLine( " \nError: {0} " , e.Message);
}
}
}
}
}
程序主要生成一个圆和一个文本,并将结果保存到文件d:/test.dwg中,效果如下图所示:
Figure 2.1 DWG generated by OpenDWG
从上面的代码及生成结果可以看出,OpenDWG的用法还是很简单的,封装得简单易用。
3.PML Proxy Object
AVEVA PML.Net提供的代理PML对象,使得在PML中调用C#的组件更加灵活,而且PML.Net编写的C#组件的布置更简单。如果你的程序算法复杂,且对性能有一定要求的话,可以考虑使用PML.Net来开发。下面结合上述生成DWG文件的代码,编写一个简单的处理DWG文件的PML .Net组件,来体验下用C#开发的效率。
用C#编写PML代理对象也是很简单的,下面直接给出代码,对于PML.Net的使用可参考以前发的blog:
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using Aveva.PDMS.PMLNet;
using DWGdirect.Runtime;
using DWGdirect.Geometry;
using DWGdirect.DatabaseServices;
namespace eryar.pmlnet.dwg
{
/// <summary>
/// a simple PML.Net object to test OpenDWG.
/// <author> Shing Liu([email protected]) </author>
/// </summary>
[PMLNetCallable()]
public class DwgTest
{
[PMLNetCallable()]
public DwgTest()
{
}
[PMLNetCallable()]
public void Assign(DwgTest rhs)
{
}
[PMLNetCallable()]
public void RunTest()
{
// initialize DWGdirect.
using (Services aService = new Services())
{
try
{
using (Database aDatabase = new Database())
{
using (BlockTable aBlockTable = (BlockTable)aDatabase.BlockTableId.GetObject(OpenMode.ForRead))
{
ObjectId aModelSpaceId = aBlockTable[BlockTableRecord.ModelSpace];
using (BlockTableRecord aRecord = (BlockTableRecord)aModelSpaceId.GetObject(OpenMode.ForWrite))
{
// make a cirlce.
Circle aCircle = new Circle();
aCircle.SetDatabaseDefaults(aDatabase);
aCircle.Radius = 50.0 ;
aRecord.AppendEntity(aCircle);
// make a text.
DBText aText = new DBText();
aText.SetDatabaseDefaults(aDatabase);
aText.Position = new Point3d( - 35.0 , 10.0 , 0.0 );
aText.TextString = " Hello World! by PML.Net " ;
aText.Height = 5.0 ;
aText.WidthFactor = 0.7 ;
aRecord.AppendEntity(aText);
}
}
aDatabase.SaveAs( " d:/test.dwg " , DwgVersion.vAC18);
MessageBox.Show( " Test DWG finished! " );
}
}
catch (System.Exception e)
{
Console.WriteLine( " \nError: {0} " , e.Message);
}
}
}
}
}
将生成的dll文件放到PDMS的安装目录中就可以在PML中调用了。调用代码如下所示:
-- Copyright (C) 2015 Shing Liu All Rights Reserved .
--
-- File : testdwg . pmlmac
-- Type : Macro Definition
-- Group : Application
-- Keyword : Test PML . Net and DWG .
-- Module : ANY
--
-- Author : Shing Liu(eryar@ 163 . com)
-- Created : 2015 - 08 - 01 08 : 32
--
-- Description : Test the OpenDWG in PML . Net .
--
-------------------------------------------------------------------------------
import ' eryar.pmlnet.dwg '
using namespace ' eryar.pmlnet.dwg '
! aTest = object DwgTest()
! aTest . runTest()
我一般是在monitor中调试PML.Net程序,因为启动monitor速度快,如下图所示:
Figure 3.1 PML.Net object usage
生成结果如下图所示:
4.Conclusion
综上所述,OpenDWG的C#封装用起来还是很方便的,而使用PML.Net编写的PML代理对象,使得在AVEVA产品中调用C#组件更加便利。本文结合OpenDWG和PML.Net给出具体实例,为PDMS程序的开发开阔下思路。
关于DWG文件的读写,也可以不借助于OpenDWG。如果公司采购了AutoCAD,也可以直接使用AutoCAD .Net来对DWG进行操作,程序更加稳定。
5. References
1. Shing Liu. OpenCASCADE DataExchange DWG.
http://www.cppblog.com/eryar/archive/2014/10/15/208581.html
2. Shing Liu. OpenCASCADE DataExchange DXF.
http://www.cppblog.com/eryar/archive/2013/12/22/204948.html
3. Shing Liu. AVEVA PML.Net Guide.
http://www.cppblog.com/eryar/archive/2014/12/25/209307.html
4. Autodesk. AutoCAD .Net Developer’s Guide.
PDF Version: AVEVA PML.Net for DWG