.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

5 REPLIES 5
Reply
Message 1 of 6
adjg13
422 Views, 5 Replies

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

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.

 

 

 

 

 

5 REPLIES 5
Message 2 of 6
dgorsman
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.
"I don't know" is the beginning of knowledge, not the end.


Message 3 of 6
jeff
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
Message 4 of 6
dgorsman
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.
"I don't know" is the beginning of knowledge, not the end.


Message 5 of 6
Hallex
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
Message 6 of 6
adjg13
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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost