Hi everyone,
I'm developing an AddIn in C # for Microstation Connect Version 12 and I should create, by code, a rule to insert in a display rules.
If I create the following rule (REFERENCE.References.Logical Name = Attachment_1), in the Microstation environment, everything works correctly (DisplayRules1.JPG) .
If I try to create the same rule from c # Microstation I get the message "Unrecognized Criteria" (DisplayRules2.JPG)
Code Example :
"string fieldValue = "Attachment_1";
string RuleStenName = "RulSet_1"; string MyRuleCondition = $@"REFERENCE.References.Logical Name = {fieldValue}";"
Could anyone tell me what is the exact way to build this rule condition from code?
Thanks for your help,
Salvio
salvatore montella said:Could anyone tell me what is the exact way to build a rule condition from code?
More generally, I think we need advice on writing Expressions programmatically. It's easy to get an Expression wrong, but hard to get diagnostic information (as you have found).
salvatore montella said:string MyRuleCondition = $@"REFERENCE.References.Logical Name = {fieldValue}";"
It's not clear what you're writing in that statement...
$@
Please write your actual code here so we can better understand what you have: Use the Insert|Code button.
Regards, Jon Summers LA Solutions
Hi Jon, thanks for replying. Here is the whole procedure I wrote to create the display rule.
private void DiplayRulesSampleNew() { BIM.CadInputQueue keyInCommand = ustnApp.CadInputQueue; DgnFile dgnFile = BM.Session.Instance.GetActiveDgnFile(); DgnModel dgnModel = BM.Session.Instance.GetActiveDgnModel(); DgnModelRef dgnModelRef = BM.Session.Instance.GetActiveDgnModelRef(); string RuleStenName = "RulSet_1"; string MyRuleCondition = @"REFERENCE.References.Logical Name = ""Attachment_1"""; DisplayRuleSet displayRuleSet = new DisplayRuleSet(RuleStenName, dgnFile); DisplayRulesManager.WriteDisplayRuleSetToFile(displayRuleSet, dgnFile, true); DisplayRule displayRuleElement = new DisplayRule(MyRuleCondition, true, dgnFile); IDisplayRuleAction ruleAction = new ColorOverrideAction(5, dgnFile); displayRuleElement.AddAction(ruleAction); displayRuleSet.AddDisplayRule(displayRuleElement); DisplayRulesManager.WriteDisplayRuleSetToFile(displayRuleSet, dgnFile, true); keyInCommand.SendKeyin("MDL KEYIN CUSTOMDISPMODE DIALOG DISPLAYSTYLES"); keyInCommand.SendKeyin(@"DISPLAYSTYLE CREATE ""DISPLAY_STYLE_BY_KEYIN"""); keyInCommand.SendKeyin(@"MDL KEYIN VIEWCTRL CHANGE VIEW CUSTOM ""DISPLAY_STYLE_BY_KEYIN"" 1"); keyInCommand.SendKeyin(@"DISPLAYSTYLE SETPARAM DISPLAYRULESET ""DISPLAY_STYLE_BY_KEYIN"" ""RulSet_1"""); ustnApp.CommandState.StartDefaultCommand(); }
Jon Summers said:It's not clear what you're writing in that statement...
It's clear in my opinion, it's standard C# syntax.
In my opinion @ is unnecessary, because there is nothing that requires to interpret the string literally, but it does not cause any issue.
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Hi Salvatore,
salvatore montella said:Here is the whole procedure I wrote to create the display rule.
In my opinion your original code and the more completed code does something different.
In the first case, at runtime, the string is:
string MyRuleCondition = "REFERENCE.References.Logical Name = Attachment_1"
whereas in the second code, the string is:
string MyRuleCondition = @"REFERENCE.References.Logical Name = ""Attachment_1"""
I have not used Display Rules API often, but I remember it's quite sensitive about quotation marks and other characters, because (unfortunately) the conditions are interpreted as text in this case.
Regards,
Hi Jan,
I tried in various ways (as you can see in the attached code) but unfortunately nobody works (with or without $ or @)
//Attempt1 string MyRuleCondition = "REFERENCE.References.Logical Name = Attachment_1"; //Attempt2 string value = "Attachment_1"; string MyRuleCondition = "REFERENCE.References.Logical Name = "+ value;
salvatore montella said:I tried in various ways (as you can see in the attached code) but unfortunately nobody works (with or without $ or @)
Hmmm :-(
I am not able to find my old code when I tried Display Rules, but I think it's about to find out how exactly the condition is stored when created manually.
BTW DisplayRule class is IDisposable, your code does not contain code respecting this fact.
salvatore montella said:I tried in various ways
Attchment_1
Attachment_1
=
string MyRuleCondition = "REFERENCE.References.Logical Name=Attchment_1";
Hi Jon.Attachment_1, Attchment_1, Foo, TestName ... don't make a difference, these are just the value of the logical name of the attachment.The problem is that Microstation does not create the rule and returns the error "Unrecognize Criteria", not that Micorstation creates the rule and then cannot resolve it.I also tried with the syntax you proposed and the result is always the same unfortunately
greetings, Salvio
Hi,
I am not aware of Display Rule much, but that condition editor is surely EC. It's creating "where criteria" for EC. It follows TSQL format. Please try to enclose your value in single quote.
Like this:
string MyRuleCondition = "REFERENCE.References.Logical Name='Attchment_1'";Let me know if this works.
Mangesh.Shelar said:It's creating "where criteria" for EC. It follows TSQL format
Great tip: thanks!
Is it universally true for an EC query that we should, in effect, write a valid TSQL statement? TSQL is not mentioned in the documentation for any API.
No,
ECQuery is object graph.
For that where criteria LHS part shows with characters that are standard TSQL in text format. For e.g., look at *Advance Search* dialog of Explorer:
But when code for where criteria for your ECQuery, you need to use something like:
query.WhereClause.Add (new PropertyExpression (RelationalOperator.LIKE, purposeProp, "Ba*"));