AVEVA PML.Net for DWG
eryar@163.com
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對(duì)象,這種代理對(duì)象可以在PML中被調(diào)用,其調(diào)用方式和其他PML對(duì)象完全一樣。且PML.Net編寫的dll部署簡(jiǎn)單,不像AddIn開發(fā)出的插件那樣需要比較復(fù)雜的配置。只需要將生成的dll放到PDMS安裝目錄,即可以在PML中調(diào)用了。
使用PML.Net編寫的PML代理類可以大大方便C#開發(fā)者的開發(fā),且可使用C#相關(guān)的大量資源,甚至是C++等資源。如本文所說的OpenDWG庫,可以實(shí)現(xiàn)在PDMS中直接對(duì)DWG/DXF文件進(jìn)行讀寫。
做CAD開發(fā)的應(yīng)該都聽說過AutoCAD,而讀寫DWG文件的庫,除了Autodesk公司的以外,還有一個(gè)組織開發(fā)出來的OpenDWG,這個(gè)組織的名字是Open Design Alliance(ODA)。因?yàn)镈WG文件廣泛使用且沒有公布文件格式,所以才有這個(gè)ODA的存在。在AVEVA 12.0版本中無意發(fā)現(xiàn)了DWGDirect相關(guān)的庫,如圖所示:
Figure 1.1 DWGDirect_Net by Open Design Alliance
本文對(duì)如何使用PML.Net編寫代理PML類起到拋磚引玉的作用,讀者可以在此基礎(chǔ)上盡情自由發(fā)揮,編寫出更加實(shí)用的程序。
2.OpenDWG usage
既然在官方的版本中提供了DWGDirect_Net庫,那么就可以正大光明地用在自己的程序開發(fā)中來對(duì)DWG文件做些處理,如為Draft的出圖使用DWG模板、修改導(dǎo)出的DXF圖紙中的中文字體、將DXF文件直接轉(zhuǎn)換成DWG格式等等。
DWGdirect_Net的用法很簡(jiǎn)單,下面給出一個(gè)最簡(jiǎn)單的例子,更詳細(xì)的內(nèi)容可參考ODA提供的示例程序。
using System;
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(eryar@163.com)</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);
}
}
}
}
}
程序主要生成一個(gè)圓和一個(gè)文本,并將結(jié)果保存到文件d:/test.dwg中,效果如下圖所示:
Figure 2.1 DWG generated by OpenDWG
從上面的代碼及生成結(jié)果可以看出,OpenDWG的用法還是很簡(jiǎn)單的,封裝得簡(jiǎn)單易用。
3.PML Proxy Object
AVEVA PML.Net提供的代理PML對(duì)象,使得在PML中調(diào)用C#的組件更加靈活,而且PML.Net編寫的C#組件的布置更簡(jiǎn)單。如果你的程序算法復(fù)雜,且對(duì)性能有一定要求的話,可以考慮使用PML.Net來開發(fā)。下面結(jié)合上述生成DWG文件的代碼,編寫一個(gè)簡(jiǎn)單的處理DWG文件的PML .Net組件,來體驗(yàn)下用C#開發(fā)的效率。
用C#編寫PML代理對(duì)象也是很簡(jiǎn)單的,下面直接給出代碼,對(duì)于PML.Net的使用可參考以前發(fā)的blog:
using System;
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(eryar@163.com)</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中調(diào)用了。調(diào)用代碼如下所示:
-------------------------------------------------------------------------------
-- 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中調(diào)試PML.Net程序,因?yàn)閱?dòng)monitor速度快,如下圖所示:
Figure 3.1 PML.Net object usage
生成結(jié)果如下圖所示:
4.Conclusion
綜上所述,OpenDWG的C#封裝用起來還是很方便的,而使用PML.Net編寫的PML代理對(duì)象,使得在AVEVA產(chǎn)品中調(diào)用C#組件更加便利。本文結(jié)合OpenDWG和PML.Net給出具體實(shí)例,為PDMS程序的開發(fā)開闊下思路。
關(guān)于DWG文件的讀寫,也可以不借助于OpenDWG。如果公司采購了AutoCAD,也可以直接使用AutoCAD .Net來對(duì)DWG進(jìn)行操作,程序更加穩(wěn)定。
5. References
1. Shing Liu. OpenCASCADE DataExchange DWG.
http://www.shnenglu.com/eryar/archive/2014/10/15/208581.html
2. Shing Liu. OpenCASCADE DataExchange DXF.
http://www.shnenglu.com/eryar/archive/2013/12/22/204948.html
3. Shing Liu. AVEVA PML.Net Guide.
http://www.shnenglu.com/eryar/archive/2014/12/25/209307.html
4. Autodesk. AutoCAD .Net Developer’s Guide.