Revit API

Reply
Active Contributor
mfleming.stantec
Posts: 32
Registered: ‎10-06-2009
Message 1 of 4 (468 Views)
Accepted Solution

C# - Loop with TextNote Type not working.

468 Views, 3 Replies
09-11-2012 10:18 PM

Hi.

 

I'm working on a simple add-in that creates a bunch of text styles in a project or family in Revit 2013.

 

I'm able to only duplicate and add 1 text type and modify it's text parameters.  I'm trying to figure out how to loop it based on an array count.

 

I want to count & loop depending the the array count for "txtHgt".

But I cannot add a variable counter to "noteType" which is a TextNoteType and this is causing the issue.

 

                // Action Code Starts Here
                Document doc = commandData.Application.ActiveUIDocument.Document;

                /// Retrieve access to all TextNote Elements
                FilteredElementCollector collectorUsed

                  = new FilteredElementCollector(doc);

                ICollection<ElementId> textNotes
                  = collectorUsed.OfClass(typeof(TextNote))
                    .ToElementIds();

                foreach (ElementId textNoteid in textNotes)
                {
                    TextNote textNote = doc.GetElement(
                      textNoteid) as TextNote;
                  
                    Transaction trans = new Transaction(doc);
                    trans.Start("ChangeParam");

                    //Setting the Variables
                    string txtOPR = "_";
                    string[] txtHgt = { "2.38mm (3/32in)", "1.98mm (5/64in)" };
                    string[] txtbkgd = { "Op", "Tr" };
                    //string txtType = (txtHgt[1] + txtOPR + txtbkgd[1]);
                    //int txtCount = txtHgt.Count;
                    int txtCount = 1;

                    for (int i = 0; i < txtCount; i++)
                       // for (int i = 0; i < list.Count; i++)
                    {
                        //string txtvar = list[i] as string;
                        //Element num = textNote.TextNoteType.Duplicate(txtvar);
                        //TextNoteType noteType4 = num as TextNoteType;

                        string txtType = (txtHgt[i] + txtOPR + txtbkgd[1]);


                        // Create a duplicate
                        Element ele = textNote.TextNoteType.Duplicate(txtType);
                        TextNoteType noteType = ele as TextNoteType;

                        // Change font parameter
                        if (null != noteType)
                        {
                            // Text Colour (only able to modify RGB Red)
                            byte valRed = 0; //Set colour to black
                            //noteType.get_Parameter("Color").Set(4);
                            noteType.get_Parameter("Color").Set(valRed);
                            // Text Line Weight 1-16
                            noteType.get_Parameter("Line Weight").Set(2);
                        }
                    }
                    trans.Commit();
                }
                // Return Success

 

 

Any Suggestions?

I have completed the project.

Not 100% clean, but it works.

 

I had to add some if statements to check for values and increase array values as needed.

 

Completed code.

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Text;
using System.Linq;
namespace CreateTextType
{
[Transaction(TransactionMode.Manual)]
public class cmdTextType : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
try
{
// Action Code Starts Here
Document doc = commandData.Application.ActiveUIDocument.Document;
/// Retrieve access to all TextNote Elements
FilteredElementCollector collectorUsed
= new FilteredElementCollector(doc);
ICollection<ElementId> textNotes
= collectorUsed.OfClass(typeof(TextNote))
.ToElementIds();
foreach (ElementId textNoteid in textNotes)
{
TextNote textNote = doc.GetElement(
textNoteid) as TextNote;
Transaction trans = new Transaction(doc);
trans.Start("ChangeParam");
//Setting the Variables
string txtOPR = "_";
string[] txtHgt = { "1.58mm (1/16in)", "1.98mm (5/64in)", "2.38mm (3/32in)", "3.17mm (1/8in)", "4.76mm (3/16in)", "6.35mm (1/4in)", "12.7mm (1/2in)" };
string[] txtbkgd = { "Op", "Tr"};
int bkgCount = 0;
int txtCount = txtHgt.Length * txtbkgd.Length;
for (int i = 0; i < txtCount; i++)
{
if (i < txtHgt.Length && bkgCount == 0)
{
bkgCount = 0;
}
else
{
if (bkgCount == 0)
{
i = 0;
}
bkgCount = 1;
if (i == txtHgt.Length && bkgCount == 1)
{
//break;
goto IfDone;
}
}
string txtType = (txtHgt[i] + txtOPR + txtbkgd[bkgCount]);
// Create a duplicate
Element ele = textNote.TextNoteType.Duplicate(txtType);
TextNoteType noteType = ele as TextNoteType;
// Change font parameter
if (null != noteType)
{
noteType.get_Parameter("Text Size").Set((3.0 / 32.0) * (1.0 / 12.0));
// Text Tab Size for 3/32" = (3.0 / 32.0) * (1.0 / 12.0)
}
}
IfDone:
trans.Commit();
}
// Return Success
return Result.Succeeded;
}
catch (Exception ex)
{
// Failure Message
message = ex.Message;
return Result.Failed;
}
}
}
}

 

 

Employee
saikat
Posts: 185
Registered: ‎08-16-2007
Message 2 of 4 (458 Views)

Re: C# - Loop with TextNote Type not working.

09-14-2012 01:55 PM in reply to: mfleming.stantec

Hi

 

I think the problem is with the count in the loop. You should change the code to -

 

...

//Setting the Variablesstring txtOPR = "_";

 

string[] txtHgt = { "2.38mm (3/32in)", "1.98mm (5/64in)"};

 

string[] txtbkgd = { "Op", "Tr"};

 

//string txtType = (txtHgt[1] + txtOPR + txtbkgd[1]);//int txtCount = txtHgt.Count;inttxtCount = 2;

 

for (inti = 0; i < txtCount; i++)

 

// for (int i = 0; i < list.Count; i++)

{

//string txtvar = list[i] as string;//Element num = textNote.TextNoteType.Duplicate(txtvar);//TextNoteType noteType4 = num as TextNoteType;stringtxtType = (txtHgt[i] + txtOPR + txtbkgd[i]);

 

// Create a duplicateElementele = textNote.TextNoteType.Duplicate(txtType);

 

TextNoteType noteType = ele asTextNoteType;

 

// Change font parameterif (null!= noteType)

{

// Text Colour (only able to modify RGB Red)byte valRed = 0; //Set colour to black//noteType.get_Parameter("Color").Set(4);

noteType.get_Parameter(

"Color").Set(valRed);

 

// Text Line Weight 1-16

noteType.get_Parameter(

"Line Weight").Set(2);

}

}

trans.Commit();

...''

 

After the above changes, I can see both the textnote types created:

 

textnotetype.png

 

Hope this helps.

cheers



Saikat Bhattacharya

Active Contributor
mfleming.stantec
Posts: 32
Registered: ‎10-06-2009
Message 3 of 4 (451 Views)

Re: C# - Loop with TextNote Type not working.

09-15-2012 12:19 AM in reply to: saikat

The loop is now working.

 

I'm trying to adjust the loop count based on the number of possible text sizes.

I'm using the following:

                    //Setting the Variables
                    string txtOPR = "_";
                    string[] txtHgt = { "1.58mm (1/16in)", "1.98mm (5/64in)", "2.38mm (3/32in)", "3.17mm (1/8in)", "4.76mm (3/16in)", "6.35mm (1/4in)", "12.7mm (1/2in)" };
                    string[] txtbkgd = { "Op", "Tr" };
                    
                    int txtCount = txtHgt.Length * txtbkgd.Length;

 Which works fine.

The next part is changeing the txtType "string value based on the specific location of the count.

I want to be ablt to change the entered Text Style Type Name after I have completed all possible values for txtHgt & txtbkgd[0].  After I enter those, I want to enter txtHgt & txtBkgd[1].

 

I'm using the following statement.

 

                    for (int i = 0; i < txtCount; i++)
                    {
                        if (i <= txtHgt.Length)
                        {
                            string txtType = (txtHgt[i] + txtOPR + txtbkgd[0]);
                        }
                        else
                        {
                            string txtType = (txtHgt[i] + txtOPR + txtbkgd[1]);
                        }

 

The name 'txtType' does not exist in the current context Error:

 

Which is saying it's outside the current scope or not defined.

What am I missing?

 

Matt

 

Active Contributor
mfleming.stantec
Posts: 32
Registered: ‎10-06-2009
Message 4 of 4 (439 Views)

Re: C# - Loop with TextNote Type not working.

09-15-2012 10:14 PM in reply to: mfleming.stantec

I have completed the project.

Not 100% clean, but it works.

 

I had to add some if statements to check for values and increase array values as needed.

 

Completed code.

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Text;
using System.Linq;

namespace CreateTextType
{
    [Transaction(TransactionMode.Manual)]
    public class cmdTextType : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData,
                                ref string message,
                                ElementSet elements)
        {
            try
            {
                // Action Code Starts Here
                Document doc = commandData.Application.ActiveUIDocument.Document;

                /// Retrieve access to all TextNote Elements
                FilteredElementCollector collectorUsed

                  = new FilteredElementCollector(doc);

                ICollection<ElementId> textNotes
                  = collectorUsed.OfClass(typeof(TextNote))
                    .ToElementIds();

                foreach (ElementId textNoteid in textNotes)
                {
                    TextNote textNote = doc.GetElement(
                      textNoteid) as TextNote;
                  
                    Transaction trans = new Transaction(doc);
                    trans.Start("ChangeParam");

                    //Setting the Variables
                    string txtOPR = "_";
                    string[] txtHgt = { "1.58mm (1/16in)", "1.98mm (5/64in)", "2.38mm (3/32in)", "3.17mm (1/8in)", "4.76mm (3/16in)", "6.35mm (1/4in)", "12.7mm (1/2in)" };
                    string[] txtbkgd = { "Op", "Tr"};
                    int bkgCount = 0;

                    int txtCount = txtHgt.Length * txtbkgd.Length;
                  
                    for (int i = 0; i < txtCount; i++)
                    {
                        if (i < txtHgt.Length && bkgCount == 0)
                        {
                            bkgCount = 0;
                        }
                        else
                        {
                            if (bkgCount == 0)
                            {
                                i = 0;
                            }
                            bkgCount = 1;
                            if (i == txtHgt.Length && bkgCount == 1)
                            {
                                //break;
                                goto IfDone;
                            }
                        }
                    
                        string txtType = (txtHgt[i] + txtOPR + txtbkgd[bkgCount]);

                        // Create a duplicate
                        Element ele = textNote.TextNoteType.Duplicate(txtType);
                        TextNoteType noteType = ele as TextNoteType;


                        // Change font parameter
                        if (null != noteType)
                        {
                            noteType.get_Parameter("Text Size").Set((3.0 / 32.0) * (1.0 / 12.0));
                            // Text Tab Size for 3/32" = (3.0 / 32.0) * (1.0 / 12.0)



                        }
                    }
                    IfDone:
                    trans.Commit();
                }
                // Return Success
                return Result.Succeeded;

            }
            catch (Exception ex)
            {

                // Failure Message
                message = ex.Message;
                return Result.Failed;

            }
        }
    }
}

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

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