Mapping Specification for DWG/DXF (MSD) C#.NET Code Samples

Similar documents
13/10/2011. Data Integration and Interoperability. Gordon Sumerling & Maree Wilson

Using CAD Data in ArcGIS

CHAPTER 10: WEB SERVICES

COMOS. Platform COMOS Platform Interfaces. Importing and exporting data 1. XML connectors 2. Standard import "Blank XML" 3

SharePoint Integration

Computer-Aided Drafting I

DToolsX-DWG. Version: An ActiveX DLL To Retrieve DWG/DXF information. DToolsX-DWG REFERENCE MANUAL

2009 Tutorial (DB4O and Visual Studio 2008 Express)

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Web Services API Developer Guide

Even Cowboys Need Good Data City of Pendleton GIS Development

UniFinger Engine SDK Manual (sample) Version 3.0.0

MS Enterprise Library 5.0 (Logging Application Block)

Envisioneer Technical Note

AutoCAD New Icon Quick Reference

Use this translator to save ArchiCAD layouts/views in DXF/DWG format if you plan to continue

CAP VR WALKTHROUGH ENVIRONMENT: 3DS MAX INTEROPERABILITY BETWEEN PROGRAMS

Exercise 1: Python Language Basics

Software Engineering 1 EEL5881 Spring Homework - 2

How to Improve Database Connectivity With the Data Tools Platform. John Graham (Sybase Data Tooling) Brian Payton (IBM Information Management)

Geodatabase Programming with SQL

ASP.NET Programming with C# and SQL Server

MapInfo Universal Translator User Guide

Converting GIS Datasets into CAD Format

Developing a Three Tier Web Application Using ASP.NET and Open Source Object-Oriented Database DB4Objects

Building Visualization using Autodesk VIZ

To draw a line. To create a construction line by specifying two points

EJB 3.0 and IIOP.NET. Table of contents. Stefan Jäger / stefanjaeger@bluewin.ch

Using SQL Server Management Studio


IDP 150: INTRODUCTION TO AUTOCAD JANUARY TERM 2014 INSTRUCTOR: Keith A. Zaltzberg

FTP Client Engine Library for Visual dbase. Programmer's Manual

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

A Method Using ArcMap to Create a Hydrologically conditioned Digital Elevation Model

TABLE OF CONTENTS. 7. shapefile..59. shapefile shapefile

Working with the Geodatabase Using SQL

How To Draw In Autocad

PHP Integration Kit. Version User Guide

Batch Posting Service Toolkit for Microsoft Dynamics GP (build 1)

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

Introduction to AutoCAD lar 543 spring 2008

Getting Started with Telerik Data Access. Contents

CHAPTER 5 INTELLIGENT TECHNIQUES TO PREVENT SQL INJECTION ATTACKS

Embedding Maps into Microsoft Office and Microsoft SharePoint

Creating Figure Ground Maps in ArcMap 10.x: Basic procedures to download, open, manipulate and print spatial data

Chapter 23: Drafting in Worksheet View

Chapter 14 WCF Client WPF Implementation. Screen Layout

Skills for Employment Investment Project (SEIP)

See the Developer s Getting Started Guide for an introduction to My Docs Online Secure File Delivery and how to use it programmatically.

ADOBE READER AND ACROBAT

CADVANCE Release Notes for V15

How to create a SMTP plugin for ArGoSoft Mail Server,.NET edition (AMS.NET edition) using Visual Studio 2005

EMC Documentum Application Connectors Software Development Kit

K e y b o a r d s h o rt c ut s

CA Workload Automation Agent for Databases

MS Active Sync: Sync with External Memory Files

PUBLIC Supplement for J.D. Edwards

Introduction to GIS.

ArcGIS Server mashups

Package uptimerobot. October 22, 2015

SQL INJECTION ATTACKS By Zelinski Radu, Technical University of Moldova

MyChartWebPart.cs. // For SortList using System.Collections.Generic; using System.Collections; // For DataTable using System.Data;

Implementing a WCF Service in the Real World

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Upgrading a Microsoft Dynamics NAV 2009 R2 or Microsoft Dynamics NAV 2009 SP1 Database to Microsoft Dynamics NAV 2015

SQL Server An Overview

Using ilove SharePoint Web Services Workflow Action

4D Plugin SDK v11. Another minor change, real values on 10 bytes is no longer supported.

Appendix K Introduction to Microsoft Visual C++ 6.0

AutoCAD 2015 and AutoCAD LT Essentials

Introduction to Building Windows Store Apps with Windows Azure Mobile Services

Security API Cookbook

ADF Code Corner. 66. How-to color-highlight the bar in a graph that represents the current row in the collection. Abstract: twitter.

Effective Collaboration using Revit Structure and AutoCAD MEP

Using the Query Analyzer

v1.1.0 SimpleSQL SQLite manager for Unity3D echo17.com

Using EDA Databases: Milkyway & OpenAccess

Developing Solutions for Microsoft Dynamics AX in a Shared AOS Development Environment

GEOiD. and Autodesk files

如 何 在 C#.2005 中 使 用 ICPDAS I/O Card 的 DLL 檔 案

Amazon Fulfillment Web Service. Getting Started Guide Version 1.1

Conexión SQL Server C#

Introduction to AutoCAD 2010

Tutorial: Windows Mobile Application Development. Sybase Unwired Platform 2.1 ESD #2

USER GUIDE Appointment Manager

Creating Database Tables in Microsoft SQL Server

A Step by Step Guide for Building an Ozeki VoIP SIP Softphone

SQL Injection Vulnerabilities in Desktop Applications

Tutorial for Creating Resources in Java - Client

NatureServe s Environmental Review Tool

Internet Technologies. World Wide Web (WWW) Proxy Server Network Address Translator (NAT)

ZWCAD Professional vs. Standard vs. Academic

Mail Programming Topics

GEOGRAPHIC INFORMATION SYSTEMS

Package sjdbc. R topics documented: February 20, 2015

CONSTRUCTION SPECIFICATIONS

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

CHAPTER 13: INTEGRATION OPTIONS

Transcription:

Mapping Specification for DWG/DXF (MSD) C#.NET Code Samples The code samples contained herein are intended as learning tools and demonstrate basic coding techniques used to implement MSD. They were created with ObjectARX 2007 using Microsoft Visual Studio 2005, C#.NET. Error handling has been omitted for clarity. This document assumes the reader has a working knowledge of reading and writing to the DWG and or DXF format. All samples are provided "as-is" and without warranty of any kind, expressed or implied. ESRI does not assume any responsibility for their successful operation. The Open Design Alliance (ODA) is a non-profit organization funded by dues from its members. The ODA offers OpenDWG, which does not replace the DWG file format but is instead compatible with the DWG file format. The ODA is committed to maintaining compatibility between the DWGdirect libraries and the DWG file format. The aim of the ODA is to provide its membership libraries that read and write CAD files, such as DWG and DGN, or other formats where appropriate. More information regarding ODA can be found at www.opendesign.com. 1

Contents Reporting the PRJ string embedded in the DWG... 3 Working with Feature Classes... 3 Working with Attributes on Entities... 10 2

Reporting the PRJ string embedded in the DWG using System; using System.Collections.Generic; using System.Text; using System.IO; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; namespace C_SharpSamples class PRJSamples static String PRJ_RecordName = "ESRI_PRJ"; public void Get_WKT(Database db) Transaction t = db.transactionmanager.starttransaction(); // get the current editor for output Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; try // open the named object dictionary DBDictionary dict = (DBDictionary)t.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, false); // grab the xrecord with the WKT string, if it exists ObjectId objid = dict.getat(prj_recordname); Xrecord rec = (Xrecord)t.GetObject(dict.GetAt(PRJ_RecordName), OpenMode.ForRead); // write out the string ResultBuffer data = rec.data; if (data!= null) ed.writemessage(data.tostring()); t.commit(); catch ed.writemessage("no PRJ found"); t.dispose(); Working with Feature Classes using System; using System.Collections.Generic; using System.Text; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; 3

using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; namespace C_SharpSamples class FCSamples static string FC_DictName = "ESRI_FEATURES"; static string FC_Type = "FeatureType"; static string FC_Query = "FeatureQuery"; static string FC_Attr = "ESRI_Attributes"; // match the feature types to the entity types used in ArcGIS. // note: ArcGIS looks into individual elements of block inserts // this code does not. static TypedValue[] tvpoint = new TypedValue(-4, "<or"), new TypedValue(0, "POINT"), new TypedValue(0, "INSERT"), new TypedValue(0, "SHAPE"), new TypedValue(0, "HATCH"), new TypedValue(0, "PROXY"), new TypedValue(-4, "or>") ; static TypedValue[] tvpolyline = new TypedValue(-4, "<or"), new TypedValue(0, "ARC"), new TypedValue(0, "CIRCLE"), new TypedValue(0, "ELLIPSE"), new TypedValue(0, "LINE"), new TypedValue(0, "MLINE"), new TypedValue(0, "*POLYLINE"), new TypedValue(0, "RAY"), new TypedValue(0, "SPLINE"), new TypedValue(0, "XLINE"), new TypedValue(0, "TRACE"), new TypedValue(0, "SOLID"), new TypedValue(0, "FACE"), new TypedValue(-4,"or>") ; static TypedValue[] tvpolygon = new TypedValue(-4, "<or"), new TypedValue(0, "CIRCLE"), new TypedValue(0, "SOLID"), new TypedValue(0, "ELLIPSE"), new TypedValue(0, "FACE"), new TypedValue (-4, "<and"), new TypedValue(0, "*POLYLINE"), new TypedValue(70, 1), new TypedValue(-4, "and>"), new TypedValue (-4, "<and"), new TypedValue(0, "MLINE"), new TypedValue(70, 1), new TypedValue(-4, "and>"), new TypedValue(-4, "or>") ; 4

TypedValue[] tvannotation = new TypedValue(-4, "<or"), new TypedValue(0, "TEXT"), new TypedValue(0, "MTEXT"), new TypedValue(0, "ATTRIBUTE"), new TypedValue(0, "ATTDEF"), new TypedValue(-4, "or>") ; TypedValue[] tvmultipatch = tvpolygon; * Create a feature class in the specified drawing * - assume feature class name = MyFeatureClass * feature class type = Polyline * query = all features on Layer1 or Layer2 public void CreateFeatureClass(Database db) Transaction t = db.transactionmanager.starttransaction(); Document ThisDrawing =Application.DocumentManager.GetDocument(db); DocumentLock doclock = ThisDrawing.LockDocument(); DBDictionary fc = OpenFeatureClassDictionary(db, "MyFeatureClass", t, OpenMode.ForWrite); // add the type // Valid values for type are // Polyline, Point, Annotation, Point, MultiPatch Xrecord rectype = new Xrecord(); rectype.data = new ResultBuffer(new TypedValue((int)DxfCode.Text, "Polyline")); fc.setat(fc_type, rectype); t.addnewlycreateddbobject(rectype, true); // add the query Xrecord recquery = new Xrecord(); recquery.data = new ResultBuffer(new TypedValue(8, "Layer1, Layer2")); fc.setat(fc_query, recquery); t.addnewlycreateddbobject(recquery, true); // commit the transaction doclock.dispose(); t.commit(); * Add attribute definitions to the previously defined feature class * - assume feature class name = MyFeatureClass * - add fields for: * Integer named IntField with a default value of 99 * Long named LongField with a default value of 900000 * Real named RealField with a default value of 99.999 * Text named TextField with a default value of * Sample String and a length of 64 characters public void AddAttributeDefinitions(Database db) 5

Transaction t = db.transactionmanager.starttransaction(); Document ThisDrawing = Application.DocumentManager.GetDocument(db); DocumentLock doclock = ThisDrawing.LockDocument(); // open/create the attribute Dictionary DBDictionary fcattr = OpenFeatureClassAttributeDictionary(db, "MyFeatureClass", t, OpenMode.ForWrite); // add an integer field with default value of 99 Xrecord recintfield = new Xrecord(); recintfield.data = new ResultBuffer(new TypedValue(70, 99)); fcattr.setat("intfield", recintfield); t.addnewlycreateddbobject(recintfield, true); // add a long field with default value of 900000 Xrecord reclongfield = new Xrecord(); reclongfield.data = new ResultBuffer(new TypedValue(90, 900000)); fcattr.setat("longfield", reclongfield); t.addnewlycreateddbobject(reclongfield, true); // add a real field with default value of 99.999 Xrecord recrealfield = new Xrecord(); recrealfield.data = new ResultBuffer(new TypedValue(40, 99.999)); fcattr.setat("realfield", recrealfield); t.addnewlycreateddbobject(recrealfield, true); // add a string field default value of "Sample String" and a length of 64 Xrecord recstringfield = new Xrecord(); recstringfield.data = new ResultBuffer(new TypedValue(1, "Sample String")); recstringfield.data.add(new ResultBuffer(new TypedValue(90, 64))); fcattr.setat("textfield", recstringfield); t.addnewlycreateddbobject(recstringfield, true); // release the lock and transaction doclock.dispose(); t.commit(); * List all the feature classes in the specified drawing * Lists the name, type, and query to the command line public void ListFeatureClasses(Database db) // get the current editor for output Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; Transaction t = db.transactionmanager.starttransaction(); Document ThisDrawing = Application.DocumentManager.GetDocument(db); DocumentLock doclock = ThisDrawing.LockDocument(); // open the feature data (obviously, you'd want to handle // the case where the feature collection doesn't exist) DBDictionary fccollection = OpenFeatureDatasetDictionary(db, t, OpenMode.ForRead); 6

foreach (DBDictionaryEntry curdict in fccollection) ed.writemessage("name: " + curdict.key + Environment.NewLine); DBDictionary thisdict = OpenFeatureClassDictionary(db, curdict.key, t, OpenMode.ForRead); // get the feature type Xrecord xtype = (Xrecord)t.GetObject(thisDict.GetAt("FeatureType"), OpenMode.ForRead, false); foreach (TypedValue tv in xtype.data.asarray()) ed.writemessage(string.format("featuretype = 1", tv.typecode, tv.value) + Environment.NewLine); // get the feature query Xrecord xquery = (Xrecord)t.GetObject(thisDict.GetAt("FeatureQuery"), OpenMode.ForRead, false); ResultBuffer rbquery = xquery.data; foreach (TypedValue tv in xquery.data.asarray()) ed.writemessage(string.format("typecode=0, Value=1", tv.typecode, tv.value) + Environment.NewLine); // release the lock and transaction doclock.dispose(); t.dispose(); * Select all the features in the feature class "MyFeatureClass" * public void SelectFeatures(Database db) Transaction t = db.transactionmanager.starttransaction(); Document ThisDrawing = Application.DocumentManager.GetDocument(db); DocumentLock doclock = ThisDrawing.LockDocument(); String fcname = "MyFeatureClass"; // get the query from the dictionary DBDictionary thisdict = OpenFeatureClassDictionary(db, fcname, t, OpenMode.ForRead); if (thisdict!= null) string typestring = ""; // get the feature type Xrecord xtype = (Xrecord)t.GetObject(thisDict.GetAt("FeatureType"), OpenMode.ForRead, false); foreach (TypedValue tv in xtype.data.asarray()) typestring = tv.value.tostring(); // get the feature query Xrecord xquery = (Xrecord)t.GetObject(thisDict.GetAt("FeatureQuery"), OpenMode.ForRead, false); 7

ResultBuffer rbfilter = new ResultBuffer(new TypedValue(-4, "<and")); // first select the feature type // Valid values for type are Polyline, "Polygon", Point, // Annotation, Point, MultiPatch if (typestring == "Polyline") for (int j = 0; j < tvpolyline.length; j++) rbfilter.add(tvpolyline[j]); else if (typestring == "Point") for (int j = 0; j < tvpoint.length; j++) rbfilter.add(tvpoint[j]); else if (typestring == "Polygon") for (int j = 0; j < tvpolygon.length; j++) rbfilter.add(tvpolygon[j]); else if (typestring == "Annotation") for (int j = 0; j < tvannotation.length; j++) rbfilter.add(tvannotation[j]); else if (typestring == "MultiPatch") for (int j = 0; j < tvmultipatch.length; j++) rbfilter.add(tvmultipatch[j]); TypedValue[] values = xquery.data.asarray(); for (int i = 0; i < values.length; i++) rbfilter.add(values[i]); rbfilter.add(new TypedValue(-4, "and>")); SelectionFilter filterss = new SelectionFilter(rbFilter.AsArray()); Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; PromptSelectionResult selresults = ed.selectall(filterss); if (selresults.status == PromptStatus.OK && selresults.value.count > 0) ObjectId[] ids = selresults.value.getobjectids(); Autodesk.AutoCAD.Internal.Utils.SelectObjects(ids); t.dispose(); doclock.dispose(); * Delete the "MyFeatureClass" definition * Note that this does not remove any attributes from entities * participating in the MyFeatureClass query void DeleteFeatureClass(Database db) 8

// begin the transaction Transaction t = db.transactionmanager.starttransaction(); Document ThisDrawing = Application.DocumentManager.GetDocument(db); DocumentLock doclock = ThisDrawing.LockDocument(); // open the dictionary DBDictionary thisdict = OpenFeatureClassDictionary(db, "MyFeatureClass", t, OpenMode.ForWrite); // erase it thisdict.erase(); // commit the transaction t.commit(); doclock.dispose(); //============================================================================= // HELPER FUNCTIONS * Helper function to open or create the Feature Class dictionary DBDictionary OpenFeatureDatasetDictionary(Database db, Transaction t, OpenMode mode) DBDictionary fccollection; DBDictionary NOD = (DBDictionary)t.GetObject(db.NamedObjectsDictionaryId, mode, false); // if it exists, just open it and return try fccollection = (DBDictionary)t.GetObject(NOD.GetAt(FC_DictName), mode, false); return fccollection; // otherwise, if we are to open for write, add it catch if (mode!= OpenMode.ForWrite) return null; fccollection = new DBDictionary(); NOD.SetAt(FC_DictName, fccollection); t.addnewlycreateddbobject(fccollection, true); fccollection = (DBDictionary)t.GetObject(NOD.GetAt(FC_DictName), mode, false); return fccollection; * Helper function to open or create a feature class dictionary DBDictionary OpenFeatureClassDictionary(Database db, string name, Transaction t, OpenMode mode) DBDictionary ffcollection = OpenFeatureDatasetDictionary(db, t, mode); 9

try DBDictionary fc = (DBDictionary)t.GetObject(ffCollection.GetAt(name), mode, false); return fc; catch if (mode == OpenMode.ForWrite) DBDictionary fc = new DBDictionary(); ffcollection.setat(name, fc); t.addnewlycreateddbobject(fc, true); fc = (DBDictionary)t.GetObject(ffCollection.GetAt(name), mode, false); return fc; return null; * Helper function to open or create a attribute definition * dictionary in a specified feature class DBDictionary OpenFeatureClassAttributeDictionary(Database db, string name, Transaction t, OpenMode mode) DBDictionary featureclass = OpenFeatureClassDictionary(db, name, t, mode); try DBDictionary fcattr = (DBDictionary)t.GetObject(featureClass.GetAt(name), mode, false); return fcattr; catch if (mode == OpenMode.ForWrite) DBDictionary fcattr = new DBDictionary(); featureclass.setat(fc_attr, fcattr); t.addnewlycreateddbobject(fcattr, true); fcattr = (DBDictionary)t.GetObject(featureClass.GetAt(FC_Attr), mode, false); return fcattr; return null; Working with Attributes on Entities using System; 10

using System.Collections; using System.Collections.Generic; using System.Text; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; namespace C_SharpSamples class EntityAttributeSamples * To attach attributes to a specific entity, follow the * example that attaches attribute definitions to a * feature class. The only difference is that you use the * ESRI_Attributes dictionary in the entity's extension * dictionary instead of the feature class dictionary. * List all the attributes attached to the specified entity * * Important note: attributes exist independent of feature * classes. To list the attributes associated with * a specific feature class, read the attribute * definitions from the feature class, and then query the * ESRI_Attributes dictionary on this entity for the existance of * each defined attribute. If not found, the value is the * default stored in the feature class definition. void ListAttributes(Database db, ObjectId entid) Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction t = db.transactionmanager.starttransaction()) // open the entity and get the extension dictionary Entity entity = (Entity)t.GetObject(entid, OpenMode.ForRead, true); ObjectId extdictid = entity.extensiondictionary; if ((extdictid.isnull == false) && (extdictid.iserased == false)) // open the extension dictionary DBObject tmpobj = t.getobject(extdictid, OpenMode.ForRead); DBDictionary dbdict = tmpobj as DBDictionary; if (dbdict!= null) // if the extension dictionary contains attributes, open the // attributes dictionary if (dbdict.contains("esri_attributes")) ObjectId attrdictid = dbdict.getat("esri_attributes"); if ((attrdictid.isnull == false) && (attrdictid.iserased == false)) DBObject anobj = t.getobject(attrdictid, OpenMode.ForRead); DBDictionary attrdict = anobj as DBDictionary; 11

if (attrdict!= null) // iterate through the attributes in the dictionary // and write out their values to the command line foreach (DBDictionaryEntry curentry in attrdict) ObjectId curentryid = curentry.value; DBObject curentryobj = t.getobject(curentryid, OpenMode.ForRead); Xrecord curx = curentryobj as Xrecord; if (curx!= null) if (curx.data!= null) IEnumerator iter = curx.data.getenumerator(); iter.movenext(); TypedValue tmpval = (TypedValue)iter.Current; if (tmpval!= null) ed.writemessage("\n"); ed.writemessage(curentry.m_key); ed.writemessage("("); ed.writemessage(tmpval.typecode.tostring()); ed.writemessage(") = "); ed.writemessage(tmpval.value.tostring()); t.dispose(); 12