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: 

linq where view.Name == name does not work when name is end shaggy, Why?

4 REPLIES 4
Reply
Message 1 of 5
aksaks
393 Views, 4 Replies

linq where view.Name == name does not work when name is end shaggy, Why?

The following has been working just fine to return a desired View:

 

Autodesk.Revit.DB.View theView = (from v in new FilteredElementCollector(doc)
                                              .OfClass(typeof(Autodesk.Revit.DB.View))
                                              .Cast<Autodesk.Revit.DB.View>()
                                               where v.ViewType == viewDragType && v.Name == theName
                                               select v)
                                              .FirstOrDefault();

Except when "theName", which is a string that started out life from an actual View.Name, has a trailing space character. "Trimming" theName as in changing the code to be "v.Name == theName.Trim() corrects the problem. The correct view, i.e. the view that has a trailing space in the name, is returned by the code even though theName.Trim() string is not actually the view's name. I'm pretty sure this is what is happening. How could this be? 

4 REPLIES 4
Message 2 of 5
m.vallee
in reply to: aksaks

+1

 

I just understood the exact same thing yesterday for view filters' names, and also used the Trim() method to make it work.

 

Strange behaviour.

Message 3 of 5
aksaks
in reply to: m.vallee

Well actually the application where I saw this also does similar with PhaseFilter names and Phase names but for some reason (probably none) I used the string Equals method as in:

 

 

 where phaseFltr.Name.Equals(phaseFilterName)


 where phase.Name.Equals(phaseName)

Now I'm wondering if this way behaves differently or if this is technically "bad form". I'm too lazy to check right now. I'll be adding a prophylactic here also based on your report. Thank you.  

Message 4 of 5
jeremytammik
in reply to: aksaks

Hi guys,

 

Thank you for the report.

 

Sounds a bit weird to me too.

 

Can one of you please provide a minimal reproducible case for this to pass on to the development team for further analysis?

 

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

 

Thank you!

 

Cheers,

 

Jeremy



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

Message 5 of 5
aksaks
in reply to: jeremytammik

I think the attached code as a single command shows what I ran into. It is long winded but I think this is the reproducible example you ask for as single cut and paste.

 

I cannot create a shaggy view name (text with trailing whitespace) using the Revit UI (talking 2016) and nor in code using "view.Name". For days I have been thinking I must have imagined seeing the Revit UI clearly showing a shaggy view name! It can be done in code using "view.ViewName".

 

To run the test, first create a floor plan view named "Find View With This Name". The test renames this view as "Find View With This ". Notice the trailing space.

 

[Transaction(TransactionMode.Manual)]
    public class ShaggyNameTest : IExternalCommand {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements) {
            try {
                UIApplication uiapp = commandData.Application;
                UIDocument uidoc = uiapp.ActiveUIDocument;
                Application app = uiapp.Application;
                Document doc = uidoc.Document;
                try {
                    ViewType desiredViewType = ViewType.FloorPlan;
                    String msgT = "ShaggyNameTest";
                    String theName = "Find View With This Name";
                    String theNName = "Find View With This ";
                    string msg = string.Empty;

                    msg = "About to look for a view named: \n\n";
                    msg += theName + "\t| length: " + theName.Length.ToString() + "\n\n";
                    System.Windows.Forms.MessageBox.Show(msg, msgT);

                    Autodesk.Revit.DB.View theView = (from v in new FilteredElementCollector(doc)
                    .OfClass(typeof(Autodesk.Revit.DB.View))
                    .Cast<Autodesk.Revit.DB.View>()
                     where v.ViewType == desiredViewType && v.Name == theName
                     select v)
                    .FirstOrDefault();

                    msg = "Result using v.Name == theName\n";
                    msg += "When theName is => " + theName + "\t| length: " + theName.Length.ToString() + "\n\n";
                    if (theView != null) {
                        msg += "Found view => " + theName + "\t| length: " + theName.Length.ToString() + "\n";
                        msg += "View.Name => " + theView.Name + "\t| length: " + theView.Name.Length.ToString() + "\n\n";
                        msg += "(theView == null).ToString() => " + (theView == null).ToString();
                    } else {
                        msg += "Not Found view => " + theName + "\t| length: " + theName.Length.ToString() + "\n\n";
                        msg += "(theView == null).ToString() => " + (theView == null).ToString();
                    }
                    System.Windows.Forms.MessageBox.Show(msg, msgT);

                    msg = "Now renaming the view using View.ViewName to: \n\n";
                    msg += theNName + "\t| length: " + theName.Length.ToString() + "\n\n";
                    System.Windows.Forms.MessageBox.Show(msg, msgT);
                    using (Transaction tr = new Transaction(doc, "ShaggyNameTest")) {
                        tr.Start();
                        theView.ViewName = theNName;
                        tr.Commit();
                    }

                    msg = "Using v.Name == theNName\n\nAbout to look for a view named: \n\n";
                    msg += theNName + "\t| length: " + theName.Length.ToString() + "\n\n";
                    System.Windows.Forms.MessageBox.Show(msg, msgT);

                    theView = (from v in new FilteredElementCollector(doc)
                                                 .OfClass(typeof(Autodesk.Revit.DB.View))
                                                 .Cast<Autodesk.Revit.DB.View>()
                               where v.ViewType == desiredViewType && v.Name == theNName
                               select v)
                                                 .FirstOrDefault();

                    msg = "Result using v.Name == theNName\n";
                    msg += "When theNName is => " + theNName + "\t| length: " + theNName.Length.ToString() + "\n\n";
                    if (theView != null) {
                        msg += "Found view => " + theNName + "\t| length: " + theNName.Length.ToString();
                        msg += "\n" + "View.Name => " + theView.Name + "\t| length: " + theView.Name.Length.ToString() + "\n\n";
                        msg += "(theView == null).ToString() => " + (theView == null).ToString();
                    } else {
                        msg += "Not Found view => " + theNName + "\t| length: " + theName.Length.ToString() + "\n\n";
                        msg += "(theView == null).ToString() => " + (theView == null).ToString();
                    }
                    System.Windows.Forms.MessageBox.Show(msg, msgT);

                    msg = "Using v.Name == theNName.Trim()\n\nAbout to look for a view named: \n\n";
                    msg += theNName + "\t| length: " + theName.Length.ToString() + "\n\n";
                    System.Windows.Forms.MessageBox.Show(msg, msgT);
                    
                    theView = (from v in new FilteredElementCollector(doc)
                      .OfClass(typeof(Autodesk.Revit.DB.View))
                      .Cast<Autodesk.Revit.DB.View>()
                       where v.ViewType == desiredViewType && v.Name == theNName.Trim()
                      select v)
                      .FirstOrDefault();

                    msg = "Result using v.Name == theNName.Trim()\n";
                    msg += "When theNName is => " + theNName + "\t| length: " + theNName.Length.ToString() + "\n\n";
                    if (theView != null) {
                        msg += "Found view => " + theName + "\t| length: " + theName.Length.ToString();
                        msg += "\n" + "View.Name => " + theView.Name + "\t| length: " + theView.Name.Length.ToString() + "\n\n";
                        msg += "(theView == null).ToString() => " + (theView == null).ToString();
                    } else {
                        msg += "Not Found view => " + theName + "\t| length: " + theName.Length.ToString() + "\n\n";
                        msg += "(theView == null).ToString() => " + (theView == null).ToString();
                    }
                    System.Windows.Forms.MessageBox.Show(msg, msgT);

                } catch (Exception ee) {
                    System.Windows.Forms.MessageBox.Show("Inner Try error\n\n" + ee.Message,
                                        "ShaggyNameTest Err",
                                        System.Windows.Forms.MessageBoxButtons.OK,
                                        System.Windows.Forms.MessageBoxIcon.Information);
                }

            } catch (Exception e) {
                System.Windows.Forms.MessageBox.Show("Outer Try error\n\n" + e.Message,
                    "ShaggyNameTest Err",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Information);
            }
            return Result.Succeeded;
        }
    } // end ShaggyNameTest

 

 

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


Rail Community