.NET

Reply
Active Contributor
adjg13
Posts: 30
Registered: ‎07-10-2013
Message 1 of 6 (198 Views)

Trying to write a layer gather and change program in C#

198 Views, 5 Replies
08-02-2013 12:43 PM

Hello Everyone, I am trying to write a layer gathering and changing program that will store a lists of layers based on layers they need to be changed to. I am writing it in C# and the part that is killing me is I need to store the old layers before I convert them so that way in the future the program will automatically know to change those layers to the appropriate layer.

 

I am trying to use an .xml file to permanently store the layers, and a serializer to read the layers into a dictionary so that I can search and see if I have already come across a layer.

 

I know that sounds very convoluted so let me simplify the problem. We have our layers which are only 7 layers. We get drawings from outside sources and they have their layers. Their layers correspond to one our 7 layers. We have a list showing which of their layers correspond to one of our layers. When we get drawings with layers we've never seen before, we need to search through the list to confirm it isn't already there and if it is not present, then ask the user to which of our layers their new layer should be associated.

 

I want to store the data in a xml file in a manner similar to this

 

<ourlayertheirlayer>
  ourlayer1, somerandomthintg
  ourlayer1, anotherrandomthing
  ourlayer1, thisrandomthing
  ourlayer2, whatisthis
  ourlayer2, whoisthis
  ourlayer3, whycantifigurethisout
  ourlayer3, canyoufigureitout
</ourlayertheirlayer>

 Then I want to serialize the xml file and save the data in a Dictionary<string,string> type whre the first string is one of our layers and the second string is their layer.

 

I want the data in the xml file to persist between application sessions, but the dictionary would be filled each the program is started.

 

Later in the program I will use the Dictionary to gather all the objects based on their layer, the second string in the dictionary, and convert them to our corresponding layer, the first string in the dictionary.

 

I am having trouble setting up my types in C# though. How do I create an xml serializer that will read ourlayer, theirlayer from an xml file and save it to a Dictionary. The data doesn't have to be in the xml file in that format either, I just can't think of an easier way than commas to sepearte the two pieces of data.

 

I was thinking something like this.

 

public class OursTheirs
{
  public List<string>[,] goToDict { get; set; }
}

public class oursTheirsComma
{
  public string commaPresent { get; set; }
}

public static class SerializeAddToDict
{
  public static string getFromXML(string xml)
  {
    XmlSerializer xmls = new XMLSerializer(typeof(string));
    return xmls.Deserialize(new StringReader(xml)) as string;
  }
  public static List<string>[,] ParseXML
  {
  //Stuff here I haven't figured out to convert the string to a
  //List<string>[,] where the first column is one of our 7 layers,second
  //column their layer
  }

  //Then some other stuff here to add the stuff to a dictionary
}

 I just starting learning C# and .NET about 2 weeks ago so please forgive any obvious errors or incorrect casing.

 

 

 

 

 

*Expert Elite*
dgorsman
Posts: 5,367
Registered: ‎10-12-2006
Message 2 of 6 (187 Views)

Re: Trying to write a layer gather and change program in C#

08-02-2013 01:57 PM in reply to: adjg13

Your XML isn't well-formed.  All data should either be from an element or an attribute of an element e.g.

 

<LayerList>
   <Layer>
      <OurName>Layer1</OurName>
      <TheirName>LayerA</TheirName>
   </Layer>
</LayerList>

Makes finding information with XPath far easier.  No point mixing in CSV values if you already have XPath.

 

Don't worry about serializing/deserializing XML - thats what the System.XML namespace is for.  You can read, parse, save, and manipulate XML content with ease.

 

Keeping a working copy of the information in an active drawing will likely be done with Extension Dictionaries and XRecords rather than raw C# data like dictionaries.  If you aren't familiar with AutoCAD Extension Dictionaries (no, its *not* the same as a Dictionary<string,string>) and XRecords you will need to read up on them first.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


Valued Mentor
jeff
Posts: 330
Registered: ‎05-12-2009
Message 3 of 6 (183 Views)

Re: Trying to write a layer gather and change program in C#

08-02-2013 02:33 PM in reply to: adjg13

If you want to persist the data between sessions in a XML or binary file you can just create a class or classes that represents the data and make it serlizable. Then just serilize an instance.

 

Then you do not have to worry about the details of how it gets persisted and not let persiting your data be such a driving force on how you have to work with it.

 

 

 

You can also find your answers @ TheSwamp
*Expert Elite*
dgorsman
Posts: 5,367
Registered: ‎10-12-2006
Message 4 of 6 (179 Views)

Re: Trying to write a layer gather and change program in C#

08-02-2013 02:36 PM in reply to: jeff

Even that seems a bit of extra work when there is the load (string) and save (string) methods of the System.XML.XMLDocument object.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 6 (148 Views)

Re: Trying to write a layer gather and change program in C#

08-03-2013 11:49 AM in reply to: adjg13

Not sure about I've understand you right,

attached just quick console app example how to write

then read the pairs of layers

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
adjg13
Posts: 30
Registered: ‎07-10-2013
Message 6 of 6 (108 Views)

Re: Trying to write a layer gather and change program in C#

08-06-2013 11:48 AM in reply to: adjg13

Thank you. I have stepped away from this for a while, but I am going back to it. I will try this method out.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.