Set the layer map table using txt file by API

Anonymous

Set the layer map table using txt file by API

Anonymous
Not applicable

Hello everyone,

Do any one know how to set the layermap (LayerTabel)  using TXT by API?.

I need to import layer mapping txt for the DWG export by the API.

 

i already know how to set the property for one category but i need to know how to import it from a file.

DWGExportOptions dwgOptions = DWGExportOptions.GetPredefinedOptions(document, setupNames[0]);

ExportLayerTable layerTable = dwgOptions.GetExportLayerTable();

ExportLayerInfo targetInfo = layerTable[targetKey];

targetInfo.ColorName = "31";

targetInfo.CutColorNumber = 31;

0 Likes
Reply
Accepted solutions (2)
2,841 Views
16 Replies
Replies (16)

matthew_taylor
Advisor
Advisor

Hi @Anonymous,

It looks like you can create your own text file parser and use the ExportLayerTable class to create one (for Revit 2017+).

 

I *think* this still works for all versions (try it manually before you code it if you try this!):

revitIniFile.WriteString("Directories", "ExportLayersNameDWG", newValue)

Where revitIniFile is the Revit.ini, "Directories" is the [Directories] section, and the other bits look like:

ExportLayersNameDWG=newValue

 

A link (there will be many more) to ways of reading/writing ini files:

https://stackoverflow.com/questions/217902/reading-writing-an-ini-file


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?

Anonymous
Not applicable

Thank you for your reply .

But it seems that i need the reverse  .

i need to import the layer mapping from custom txt file.

Import it not export it.

By the way i am working with Revit 2014

0 Likes

matthew_taylor
Advisor
Advisor

@Anonymous,

(It looks like I was wrong about the version - the ExportLayerTable  method works for Revit 2014.)

 

Well, I think we're talking about the same thing. The key thing you're omitting is what the custom text file is formatted like. Parsing the file seems to be the only issue. So, what does the text file look like?


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes

Anonymous
Not applicable

The file was exported from Revit earlier.

I want to ensure that it will be loaded before every extraction.

we use a tool to extract the DWG and it is erasing the setup for the extract each time.

so i am trying to ensure that before the extraction it will load the correct mapping

the file looks like:

 

 

 

# Revit Export Layers
# Maps Categories and Subcategories to layer names and color numbers
# Category <tab> Subcategory <tab> Layer name <tab> Color number <tab>
# Cut layer name <tab> Cut color number
# Do not remove the colon (:) after certain category names.
# -----------------------------------------------------
Adaptive Points A-Z81-M_Non_Plot 150
Adaptive Points Lines A-Z81-M_Non_Plot 150
Adaptive Points Planes A-Z81-M_Non_Plot 150
Adaptive Points Points A-Z81-M_Non_Plot 150
Air Terminal Tags A-G63-A_Ductwork 70
Air Terminals A-G63-M_Ductwork 70
Area Load Tags A-F90-A_Area 13
Area Polylines A-Z75-H_Area_Fill 32
Area Tags A-F90-A_Area 13
Areas A-F90-A_Area 13
Areas Color Fill A-F90-H_Area 13
Areas Interior Fill A-Z75-H_Area_Fill 32
Areas Reference A-F90-M_Area 32
Boundary Conditions A-Z46-M_Boundary 91
Brace in Plan View Symbols A-Z22-A_Tag 171
Cable Tray Fitting Tags A-G63-M_Cable_tray 211
Cable Tray Fittings A-G63-M_Cable_tray 211
Cable Tray Fittings Center line A-Z43-M_Centreline 70
Cable Tray Tags A-G63-A_Cable_tray 150
Cable Trays A-G63-M_Cable_tray 211
Cable Trays Center line A-Z43-M_Centreline 70

 

 

 

 

 

 

 

 

 

 

 

 

0 Likes

matthew_taylor
Advisor
Advisor

Hi @Anonymous,

Well it sounds like the tool is the problem...Have you asked for support from whomever wrote it?

 

To immediately solve your issue though, you could do either of these two things:

  1. Always have a duplicate of the valid export setting. After exporting using the tool, just create another copy of it. (I have successfully manually copy-pasted a DWG export setting, so one of the CopyElements overloads should work.)
  2. Import the layermapping using the LayerMapping property: http://www.revitapidocs.com/2018/693ca2ec-97d0-a0b4-e5f1-0691b226cfc5.htm I believe it allows the use of a file, even though the help files don't mention it. (This is backed up by this feature working using the Revit UI.)

Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes

Anonymous
Not applicable

Thanks , I am still a beginner in the Revit C# codding world .

i tried all the methods but none of them seems for importing a txt file.

or you are suggesting to read the txt file line by line then assign the keys one by one?.

0 Likes

matthew_taylor
Advisor
Advisor

Hi @Anonymous,

It's a bit bewildering to start with. Don't worry, you'll get it.

 

Method 2 would be simply this:

DWGExportOptions dwgOptions = DWGExportOptions.GetPredefinedOptions(document, setupNames[0]);
dwgOptions.LayerMapping = yourTextFileNameIncludingPath;

Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes

Anonymous
Not applicable

it seems that iam doing something wrong when i entered

 

DWGExportOptions dwgOptions = DWGExportOptions.GetPredefinedOptions(document, setupNames[0]);
            dwgOptions.LayerMapping = "C:\QRC_Uniclass_BS1192_AutoCADExport.txt" ; 

 

it give an error of:

Unrecognized escape sequence (CS1009) 

 

and when i tried to remove the "" 

 

DWGExportOptions dwgOptions = DWGExportOptions.GetPredefinedOptions(document, setupNames[0]);
            dwgOptions.LayerMapping = C:\QRC_Uniclass_BS1192_AutoCADExport.txt ; 

 

it shows :

Unexpected character '\' (CS1056) 

 

 

 

0 Likes

matthew_taylor
Advisor
Advisor
Accepted solution

Hi @Anonymous,

Codes like CS1009 are fairly unique so they are easily researchable.

Using google with the search criteria 'CS1009 c#' one of the first hits suggests putting an '@' symbol before the first ".

So, try:

dwgOptions.LayerMapping = @"C:\QRC_Uniclass_BS1192_AutoCADExport.txt" ; 

Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?

Anonymous
Not applicable

Thanks and sorry for inconvenience. 

0 Likes

Anonymous
Not applicable

I have done all what it needs but it seems it do not load the file after all.

I even tried to verify the issue if it is in saving the settings or loading the settings by showing the color of the first category but it did not work.

It build successfully but it do nothing.

the code below show the same color code before and after loading the file

 

public void layerLoader()
        {
            
             Document curDoc = this.ActiveUIDocument.Document;
             UIDocument curDocUI = this.ActiveUIDocument;
             
                  
             DWGExportOptions dwgOptions = DWGExportOptions.GetPredefinedOptions(curDoc,"EX");
             
             ExportLayerTable layerTable = dwgOptions.GetExportLayerTable();
             
             IList<ExportLayerKey> tarList = layerTable.GetKeys();
                string category = "Air Terminals";
                ExportLayerKey targetKey = layerTable.GetKeys().First<ExportLayerKey>(layerKey => layerKey.CategoryName == category);
                ExportLayerInfo targetInfo = layerTable[targetKey];
                TaskDialog.Show("test before",targetInfo.ColorName.ToString());
             
             
             dwgOptions.LayerMapping = @"C:\QRC_Uniclass_BS1192_AutoCADExport.txt" ;
             TaskDialog.Show("test","load done");
             
             
             
             layerTable = dwgOptions.GetExportLayerTable();
             
             
                 category = "Air Terminals";
                 targetKey = layerTable.GetKeys().First<ExportLayerKey>(layerKey => layerKey.CategoryName == category);
                 targetInfo = layerTable[targetKey];
                TaskDialog.Show("test before",targetInfo.ColorName.ToString());

             
             TaskDialog.Show("test","save done");
             

    
            
        } //public void layerLoader()

0 Likes

matthew_taylor
Advisor
Advisor

Hi @Anonymous,

Hm. Looks like it doesn't work as it does in the interface.

You may need to try item 1.


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes

FAIR59
Advisor
Advisor
Accepted solution

The only way I can find to "save" your imported table  and get an updated LayerTable, is to actually export a view to DWG !!

 

            dwgOptions = new DWGExportOptions();
            dwgOptions.LayerMapping = @"C:\Temp\exportlayers-dwg-KCAP_zwart_02.txt" ;

            curDoc.Export(Path.GetTempPath(),"test",new List<ElementId>(){curDoc.ActiveView.Id},dwgOptions);
            	
//            using (Transaction t = new Transaction(curDoc,"create_Settings"))
//            {
//             	t.Start();
//             	ExportDWGSettings.Create (curDoc,"API_Created_"+DateTime.Now.ToString(),dwgOptions);
//             	t.Commit();
//            }
             
            layerTable = dwgOptions.GetExportLayerTable();
            foreach ( var key in layerTable.GetKeys())
            {
            	if(layerTable[key].ColorNumber>-1)
            	{
            		string s = string.IsNullOrWhiteSpace( key.SubCategoryName)? "" : "."+key.SubCategoryName;
             		sb.AppendLine(string.Format("{0}{1} <{2}>", key.CategoryName,s,layerTable[key].ColorNumber));
            	}
            }
            TaskDialog.Show("last test",sb.ToString());

 

matthew_taylor
Advisor
Advisor

@FAIR59

AHA! I could have sworn it worked...but I guess I've never done it without doing an export before. Well done.


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes

HyungJun.K
Participant
Participant
sb.AppendLine(string.Format("{0}{1} <{2}>", key.CategoryName,s,layerTable[key].ColorNumber));

I'm not very good at coding. So I have a question. Can you tell me what sb stands for here?

0 Likes

FAIR59
Advisor
Advisor

sb is an object of the StringBuilder class.

using System.Text;

                StringBuilder sb = new StringBuilder();