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對象,這種代理對象可以在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;
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);
}
}
}
}
}
程序主要生成一個圓和一個文本,并將結果保存到文件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;
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中調用了。調用代碼如下所示:
-------------------------------------------------------------------------------
-- 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.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.