Author Topic: create Layers, is there a better way  (Read 23268 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #45 on: January 15, 2008, 04:12:14 PM »
Glenn, do you have an example of reading or writing XML?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #46 on: January 15, 2008, 04:12:56 PM »
Kerry thanks.  I'll start looking into that.  MSDN for info?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: create Layers, is there a better way
« Reply #47 on: January 15, 2008, 04:30:51 PM »
a simplistic starter
http://www.theswamp.org/index.php?topic=20033.0

.. and yes, MSDN !
there are heaps of articles and samples.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #48 on: January 15, 2008, 04:58:28 PM »
Glenn, do you have an example of reading or writing XML?

Apart from Kerry's excellent link, you might try searching for XmlReader and XmlWriter, until you get accustomed to serialising your classes to and from XML.
« Last Edit: January 15, 2008, 05:02:22 PM by Glenn R »

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #49 on: January 15, 2008, 05:02:08 PM »
I'm reading Kerry's post now.  Thanks guys
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: create Layers, is there a better way
« Reply #50 on: January 16, 2008, 08:34:30 PM »
David, have a play ...
don't let the using statements scare you, they are not ALL needed :-)

// CodeHimBelongaKwb ©  Dec 2008

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;

using System.Runtime.InteropServices;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
//
using AcCm = Autodesk.AutoCAD.Colors;
using AcGi = Autodesk.AutoCAD.GraphicsInterface;
using AcLy = Autodesk.AutoCAD.LayerManager;
using AcPl = Autodesk.AutoCAD.PlottingServices;
using AcUi = Autodesk.AutoCAD.Windows;

using WinForms = System.Windows.Forms;

[assembly: AcRx.CommandClass(typeof(LayersFromXML.TestCommands))]

namespace LayersFromXML
{
    public class TestCommands   
    {
        [AcRx.CommandMethod("Cmd2")]
        static public void test2()
        {
            Util.GetLayerXMLData();
        }

    //==========================================================
    public class Util
    {
        //---------------------------------------------------------
        public static void GetLayerXMLData()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            PromptStringOptions ops = new PromptStringOptions("\nLayer name: ");
            ops.AllowSpaces = true;

            PromptResult res = ed.GetString(ops);
           
            if (!(res.Status == PromptStatus.OK && res.StringResult != string.Empty))
            {
                return;
            }
            string layerName = res.StringResult;
            DataSet dsLayerData = new DataSet();
            dsLayerData.ReadXml(@"c:\LayerDefinitions.xml");
            DataView dsLayerDefinition = new DataView(dsLayerData.Tables["Layer"]);
            dsLayerDefinition.Sort = "LayerName";

            int rowIndex = dsLayerDefinition.Find(layerName);
            if (rowIndex == -1)
            {
                // The LayerName was not found in our LayerData
                ed.WriteMessage("\nSpecified layer not found in the file.");               
                return;
            }
            ed.WriteMessage("\nLayerName : {0}", dsLayerDefinition[rowIndex]["LayerName"]);
            ed.WriteMessage("\nDesc      : {0}", dsLayerDefinition[rowIndex]["Description"]);
            ed.WriteMessage("\nColor     : {0}", dsLayerDefinition[rowIndex]["Color"]);
            ed.WriteMessage("\nLineType  : {0}", dsLayerDefinition[rowIndex]["LineType"]);
            ed.WriteMessage("\nAlias     : {0}", dsLayerDefinition[rowIndex]["Alias"]);
        }
    }
}
 
 

and the XML file   c:\LayerDefinitions.xml
<?xml version="1.0" encoding="utf-8"?>
<LayerData>
  <Layer>
    <Description>Outlines 0.90mm</Description>
    <LayerName>ST90</LayerName>
    <Color>5</Color>
    <LineType>CONTINUOUS</LineType>
    <Alias>ST90</Alias>
  </Layer>
  <Layer>
    <LayerName>ST70</LayerName>
    <Description>Outlines 0.70mm</Description>
    <Color>3</Color>
    <LineType>CONTINUOUS</LineType>
    <Alias>ST70</Alias>
  </Layer>
  <Layer>
    <LayerName>ST50</LayerName>
    <Description>Outlines 0.50mm</Description>
    <Color>5</Color>
    <LineType>CONTINUOUS</LineType>
    <Alias>ST50</Alias>
  </Layer>
  <Layer>
    <LayerName>ST35</LayerName>
    <Description>Outlines 0.35mm</Description>
    <Color>2</Color>
    <LineType>CONTINUOUS</LineType>
    <Alias>ST35</Alias>
  </Layer>
  <Layer>
    <LayerName>ST25</LayerName>
    <Description>Outlines 0.25mm</Description>
    <Color>1</Color>
    <LineType>CONTINUOUS</LineType>
    <Alias>ST25</Alias>
  </Layer>
  <Layer>
    <LayerName>ST18</LayerName>
    <Description>Outlines 0.18mm</Description>
    <Color>4</Color>
    <LineType>CONTINUOUS</LineType>
    <Alias>ST18</Alias>
  </Layer>
</LayerData>
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #51 on: January 17, 2008, 03:40:25 AM »
Nicely done Kerry - DataView's can be very handy.

Cheers,
Glenn.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: create Layers, is there a better way
« Reply #52 on: January 17, 2008, 01:13:04 PM »
How about using a DWG file to hold your layer info? Don’t get me wrong, XML is a great idea. I just wanted to see if I could … ^-^

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #53 on: January 17, 2008, 01:17:01 PM »
I thought about that, but I was trying to come up with files that nobody would want to edit (read screw up) except me, and a dwg is too easy to open and tweak by some of my wanna-be power users.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: create Layers, is there a better way
« Reply #54 on: January 17, 2008, 01:24:13 PM »
.. too easy to open and tweak by some of my wanna-be power users.

Oh God, wanna-be power users, may I recommend a 1024 bit encryption then  :-D

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #55 on: January 17, 2008, 01:25:04 PM »
yyyeeesss please!!!
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #56 on: January 17, 2008, 01:31:11 PM »
How about using a DWG file to hold your layer info? Don’t get me wrong, XML is a great idea. I just wanted to see if I could … ^-^


I use a DWS file myself which has the added bonus of being able to be used for cad standards checking.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: create Layers, is there a better way
« Reply #57 on: January 17, 2008, 01:33:07 PM »
Glenn R,  could this have been done with Clone() or DeepClone()?

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #58 on: January 17, 2008, 01:36:44 PM »
Dan,

I would have to look up what I did exactly, but I think I used WblockClone from memory.

Cheers,
Glenn.

SomeCallMeDave

  • Guest
Re: create Layers, is there a better way
« Reply #59 on: January 17, 2008, 01:55:58 PM »
I thought about that, but I was trying to come up with files that nobody would want to edit (read screw up) except me, and a dwg is too easy to open and tweak by some of my wanna-be power users.

If you open a file using db.ReadDwgFile like Daniel did,  the file does not have to have a .dwg extension.   I just tested if (briefly, but I did test it)  and it works. 

You could 'hide' your dwg so no one would poke at it.