Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Set the layer map table using txt file by API

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
Anonymous
2840 Views, 16 Replies

Set the layer map table using txt file by API

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;

16 REPLIES 16
Message 2 of 17
matthew_taylor
in reply to: Anonymous

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?
Message 3 of 17
Anonymous
in reply to: matthew_taylor

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

Message 4 of 17
matthew_taylor
in reply to: Anonymous

@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?
Message 5 of 17
Anonymous
in reply to: matthew_taylor

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

 

 

 

 

 

 

 

 

 

 

 

 

Message 6 of 17
matthew_taylor
in reply to: Anonymous

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?
Message 7 of 17
Anonymous
in reply to: matthew_taylor

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?.

Message 8 of 17
matthew_taylor
in reply to: Anonymous

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?
Message 9 of 17
Anonymous
in reply to: matthew_taylor

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) 

 

 

 

Message 10 of 17
matthew_taylor
in reply to: Anonymous

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?
Message 11 of 17
Anonymous
in reply to: matthew_taylor

Thanks and sorry for inconvenience. 

Message 12 of 17
Anonymous
in reply to: matthew_taylor

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()

Message 13 of 17
matthew_taylor
in reply to: Anonymous

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?
Message 14 of 17
FAIR59
in reply to: Anonymous

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());

 

Message 15 of 17
matthew_taylor
in reply to: FAIR59

@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?
Message 16 of 17
HyungJun.K
in reply to: FAIR59

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?

Message 17 of 17
FAIR59
in reply to: HyungJun.K

sb is an object of the StringBuilder class.

using System.Text;

                StringBuilder sb = new StringBuilder();

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

Post to forums  

Autodesk Design & Make Report