Concatenate

Concatenate

Anonymous
Not applicable
2,538 Views
10 Replies
Message 1 of 11

Concatenate

Anonymous
Not applicable
I am new to c# programming and am trying to create a program that would concatenate 2 parameters into a single parameter on sheets. I have been able to run a foreach to pull a list for each parameter, however I am stuck on combining the 2 parameters into one. Does anyone have any suggestions?
0 Likes
2,539 Views
10 Replies
Replies (10)
Message 2 of 11

jeremytammik
Autodesk
Autodesk

Dear Wwydock,

 

Thank you for your query.

 

Please forgive a poor nerd if I tell you that parameters cannot be concatenated.

 

I assume what you mean is that you would like to concatenate their values.

 

That can easily be achieved, assuming they are strings, simply using the + operator on them:

 

  string s = "hello";
  s = s + ", " + s;

 

Now I further assume that you would like to display the concatenated value on your sheet.

 

I am not sure how that can be achieved. One way is probably to define a shared parameter, populate it with the concatenated value and display that.

 

That is probably overkill, though, and there are probably much better ways to go.

 

I hope this helps get you started, at least.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 11

Anonymous
Not applicable

One way to acheive this is:

 

Transaction t1 = new Transaction(doc, "Place Instance");
t1.Start();
string sharedParameterFile = @"D:\MySharedParameters.txt";
DefinitionFile informationFile = null;
System.IO.FileInfo documentMessage = new System.IO.FileInfo(sharedParameterFile);
bool fileExist = documentMessage.Exists;
if (!fileExist)
{
System.IO.FileStream fileFlow = System.IO.File.Create(sharedParameterFile);
fileFlow.Close();
}
app.SharedParametersFilename = sharedParameterFile;
informationFile = app.OpenSharedParameterFile();
DefinitionGroups informationCollections = informationFile.Groups;
DefinitionGroup informationCollection = null;
informationCollection = informationCollections.get_Item("MyParameters");
if (null == informationCollection)
{
informationCollections.Create("MyParameters");
informationCollection = informationCollections.get_Item("MyParameters");
}
Definition information = informationCollection.Definitions.get_Item(parameter1.name + "-" + parameter2.name);
if (null == information)
{
informationCollection.Definitions.Create(parameter1.name + "-" + parameter2.name, Autodesk.Revit.DB.ParameterType.Text);
information = informationCollection.Definitions.get_Item(parameter1.name + "-" + parameter2.name);
}
// Create a new Binding object with the categories to which the parameter will be bound
CategorySet categories = app.Create.NewCategorySet();
Category floorsClassification = null;
// use category in instead of the string name to get category
//in place of BuiltInCategory.OST_StructuralColumns specify the category that you want
floorsClassification = doc.Settings.Categories.get_Item(BuiltInCategory.OST_StructuralColumns);
categories.Insert(floorsClassification);
InstanceBinding caseTying = app.Create.NewInstanceBinding(categories);
// Add the binding and definition to the document
bool boundResult = doc.ParameterBindings.Insert(information, caseTying);

 

 

Now Populate the parameter [parameter1.name + "-" + parameter2.name] with the cocatenated values 

 

Parameter param = fi.get_Parameter(parameter1.name + "-" + parameter2.name);
param.Set(parameter1.value + "-" + parameter2.value);

 

t1.Commit();

 

I hope that helps.

 

Thanks & Regards

0 Likes
Message 4 of 11

Anonymous
Not applicable

 

This is the code i had been working with. I do want to concatenate the values for the 2 shared parameters, eventually pushing them into the Sheet name parameter. But i appear to be stuck at combining the values.

 

 

		public void MyFirstMacro()
		{
			Document doc = this.ActiveUIDocument.Document;
            string Discipline = "";
	            foreach (Element e in new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)))
	            {
	                Parameter parameter = e.get_Parameter("SHEET DISCIPLINE");
	                string title = parameter.AsString();
	                Discipline += title + "\n";
	            }
	          string Title = "";  
	            foreach (Element e in new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)))
	            {
	                Parameter parameter = e.get_Parameter("SHEET_TITLE 1");
	                string SheetName = parameter.AsString();
	                Title += SheetName + "\n";
                }
	           string sheet=Discipline+" "+Title;
	        
	            TaskDialog.Show("List",sheet);
		}

 

0 Likes
Message 5 of 11

jeremytammik
Autodesk
Autodesk

Dear Wwydock,

 

Thank you for the more detailed explanation.

 

So you have accessed and concatenated the existing parameter values and you would like to display the value of the string variable 'sheet' in a new parameter.

 

Sanjaymann basically explained all you have to do.

 

I would recommend taking the following steps:

 

1. If you have niot already done so, forget about your immediate tasks and first take a couple of hours to thouroughly work through the Revit API getting started material:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#2

 

2. Part of getting started with the Revit API includes installing the Revit SDK and documentation. Do so.

 

3. Look at the FireRating SDK sample. That demonstrates all the steps you need. The same functionality is also included in The Building Coder expanded version of the Revit API training labs, in the Xtra modules for lab 4, for C# and VB respectively, in the following external command implementations:

 

  • Lab4_3_1_CreateAndBindSharedParam
  • Lab4_3_2_ExportSharedParamToExcel
  • Lab4_3_3_ImportSharedParamFromExcel

 

Here are the GitHub links to the project and those modules:

 

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 6 of 11

Anonymous
Not applicable

Ok, I think I understand the issue. You want to loop through all sheets in the document and rename them to be the combination of "SHEET DISCIPLINE" and "SHEET_TITLE 1"?

 

If that is correct, then try this out, I fixed up your code a little to do as I describe above. Please note that I haven't tested this, so it might need a small tweak here or there, let me know.

 

 

public void MyFirstMacro()
{
	Document doc = this.ActiveUIDocument.Document;
	
	string newSheetNames = "";
	
	using(Transaction trans = new Transaction(doc, "Rename Sheets")
	{
		trans.Start();
		
		foreach (Element e in new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)))
		{
			ViewSheet sheetView = e as ViewSheet;

			Parameter paramDiscipline = sheetView.get_Parameter("SHEET DISCIPLINE");
			string sheetDiscipline = paramDiscipline.AsString();
			
			Parameter paramTitle = sheetView.get_Parameter("SHEET_TITLE 1");
			string sheetTitle = paramTitle.AsString();
		
			string combinedName = sheetDiscipline + " " + sheetTitle;
		
		
			// This will assign the new name to the sheet
			sheetView.Name = combinedName;

			newSheetNames += combinedName + "\n";
		}

		trans.Commit();
	}

	TaskDialog.Show("List",newSheetNames);
}

 

Now that I've done your homework for you, you've now got time to do all those things that Jeremy suggested 🙂

 

 

0 Likes
Message 7 of 11

Anonymous
Not applicable

I will do the homework after i get this working. I am using the sharp develop for modifying the macro, however everytime i try to run the build for i get a syntax error and missing a} at the smart and commit parts respectively. I dont what the syntax error is at the start and i keep going through  a perpetual loop of adding and removing } at the commit. I tried changing the location for the dialog box command and even removing it.

0 Likes
Message 8 of 11

Anonymous
Not applicable

Oh I just loadied this into teh macro editor and found 2 simple errors, one yours and the other mine 🙂

 

The first line should be:  Document doc = this.Document;

 

 

There was also a closing brace missing from the transaction initialisation (my fault) and should look like:

 

using(Transaction trans = new Transaction(doc, "Rename Sheets"))

 

 

That's what I get for writing code in notepad instead of an IDE with brace matching 🙂

 

 

0 Likes
Message 9 of 11

Anonymous
Not applicable

That makes sense now. I was wondering how do you filter for a particular category to create a similar macro to combine 2 parameters into 1 for my mechanical equipment. When i try substituting OfCategory(OST_BuiltInCategory.Mechanicalequipment for OfClass(Typeof(Viewsheet) i recieve errors. Am i going in the wrong direction for that?

0 Likes
Message 10 of 11

Anonymous
Not applicable

Sanjaymann,

 

Does the coding that you posted mean that it is continuously running? I did not see commit at the end of it. I substituted my parameters and changed the categories to viewsheet and i was getting errors for app, .value and .name. Is the .value and .name because my parameter is in quotes? Below is what i have.

 

 

			Document doc = this.Document;
			Transaction t1 = new Transaction(doc, "Place Instance");
				t1.Start();
				string sharedParameterFile = @"I:\Will's Stuff\My Template\For Revit\Shared Parameters\Shared Parameters.txt";
				DefinitionFile informationFile = null;
				System.IO.FileInfo documentMessage = new System.IO.FileInfo(sharedParameterFile);
				bool fileExist = documentMessage.Exists;
				if (!fileExist)
				{
				System.IO.FileStream fileFlow = System.IO.File.Create(sharedParameterFile);
				fileFlow.Close();
				}
				app.SharedParametersFilename = sharedParameterFile;
				informationFile = app.OpenSharedParameterFile();
				DefinitionGroups informationCollections = informationFile.Groups;
				DefinitionGroup informationCollection = null;
				informationCollection = informationCollections.get_Item("MyParameters");
				if (null == informationCollection)
				{
				informationCollections.Create("MyParameters");
				informationCollection = informationCollections.get_Item("MyParameters");
				}
				Definition information = informationCollection.Definitions.get_Item("SHEET DISCIPLINE".name + " " + "SHEET_TITLE 1".name);
				if (null == information)
				{
				informationCollection.Definitions.Create("SHEET DISCIPLINE".name + " " + "SHEET_TITLE 1".name, Autodesk.Revit.DB.ParameterType.Text);
				information = informationCollection.Definitions.get_Item("SHEET DISCIPLINE".name + " " + "SHEET_TITLE 1".name);
				}
				// Create a new Binding object with the categories to which the parameter will be bound
				CategorySet categories = app.Create.NewCategorySet();
				Category floorsClassification = null;
				// use category in instead of the string name to get category
				//in place of BuiltInCategory.OST_StructuralColumns specify the category that you want
				floorsClassification = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Views);
				categories.Insert(floorsClassification);
				InstanceBinding caseTying = app.Create.NewInstanceBinding(categories);
				// Add the binding and definition to the document
				bool boundResult = doc.ParameterBindings.Insert(information, caseTying);
				 
				 
				//Now Populate the parameter [parameter1.name + "-" + parameter2.name] with the cocatenated values 
				 
				Parameter param = fi.get_Parameter("SHEET DISCIPLINE".name + " " + "SHEET_TITLE 1".name);
				param.Set("SHEET DISCIPLINE".value + "-" + "SHEET_TITLE 1".value);

 

0 Likes
Message 11 of 11

Anonymous
Not applicable

Dear wwydoc,

 

You can commit the transaction once you are done with the creation of the parameters and setting their values.

 

Replace

 

Definition information = informationCollection.Definitions.get_Item("SHEET DISCIPLINE".name + " " + "SHEET_TITLE 1".name);

 

with

 

Definition information = informationCollection.Definitions.get_Item("SHEET DISCIPLINE"+ " " + "SHEET_TITLE 1");

 

Replace

 

 

informationCollection.Definitions.Create("SHEET DISCIPLINE".name + " " + "SHEET_TITLE 1".name, Autodesk.Revit.DB.ParameterType.Text);
information = informationCollection.Definitions.get_Item("SHEET DISCIPLINE".name + " " + "SHEET_TITLE 1".name);

 

With

 

informationCollection.Definitions.Create("SHEET DISCIPLINE"+ " " + "SHEET_TITLE 1", Autodesk.Revit.DB.ParameterType.Text);
information = informationCollection.Definitions.get_Item("SHEET DISCIPLINE" + " " + "SHEET_TITLE 1");

 

Replace

 

Parameter param = fi.get_Parameter("SHEET DISCIPLINE".name + " " + "SHEET_TITLE 1".name);
param.Set("SHEET DISCIPLINE".value + "-" + "SHEET_TITLE 1".value);

 

With

 

Parameter discparam= fi.get_Parameter("SHEET DISCIPLINE");

Parameter sheetparam= fi.get_Parameter("SHEET_TITLE 1");

 

Parameter param = fi.get_Parameter("SHEET DISCIPLINE"+ " " + "SHEET_TITLE 1");
param.Set(discparam.AsString() + "-" + sheetparam.AsString());

 

Hope that helps.

 

 

Thanks & Regards

 

0 Likes