Curious
Curious
CC#
Created by Curious on 9/13/2024 in #help
Is this a TableLayoutPanel?
No description
2 replies
CC#
Created by Curious on 5/23/2023 in #help
❔ XML Read XML and save as dictionary
Hi all. I want to read XML data and convert to a dictionary, I have given this go with: This is how i write:
......Above
XmlTextWriter writer = new XmlTextWriter(StoredDataFile, System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement(startingText);
//write dict to xml
foreach (KeyValuePair<string, bool> kvp in toWrite)
{
writer.WriteStartElement("Values");
writer.WriteAttributeString("key", kvp.Key);
writer.WriteAttributeString("value", kvp.Value.ToString());
writer.WriteEndElement();
}
......Below
......Above
XmlTextWriter writer = new XmlTextWriter(StoredDataFile, System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement(startingText);
//write dict to xml
foreach (KeyValuePair<string, bool> kvp in toWrite)
{
writer.WriteStartElement("Values");
writer.WriteAttributeString("key", kvp.Key);
writer.WriteAttributeString("value", kvp.Value.ToString());
writer.WriteEndElement();
}
......Below
This is how I am reading:
public static Dictionary<string, bool> ReadFromXML(string tagToRead)
{
Dictionary<string, bool> outDict = new Dictionary<string, bool>();
//look for the tag tagtoread in the XML tree, then return all values in that tag as a dictionary
try
{
XDocument doc = XDocument.Load(StoredDataFile);
XElement root = doc.Root;
var values = doc.Root.Elements(tagToRead).Select(n => n.Value);

//iterate through and get name and value to add to dictionary
foreach (var value in values)
{
string[] split = value.Split(',');
outDict.Add(split[0], Convert.ToBoolean(split[1]));
}
return outDict;

}
catch (Exception ex)
{
Autodesk.Revit.UI.TaskDialog.Show("Read XML Error", ex.Message);
return null;
}

}
public static Dictionary<string, bool> ReadFromXML(string tagToRead)
{
Dictionary<string, bool> outDict = new Dictionary<string, bool>();
//look for the tag tagtoread in the XML tree, then return all values in that tag as a dictionary
try
{
XDocument doc = XDocument.Load(StoredDataFile);
XElement root = doc.Root;
var values = doc.Root.Elements(tagToRead).Select(n => n.Value);

//iterate through and get name and value to add to dictionary
foreach (var value in values)
{
string[] split = value.Split(',');
outDict.Add(split[0], Convert.ToBoolean(split[1]));
}
return outDict;

}
catch (Exception ex)
{
Autodesk.Revit.UI.TaskDialog.Show("Read XML Error", ex.Message);
return null;
}

}
I get an error Pref is an unexpected token. The expected token is = Line 2, position 20.
25 replies
CC#
Created by Curious on 4/17/2023 in #help
❔ Properties.Settings.Default producing different stored values when called from different winForms
Environment (Autodesk Revit API) I am using Properties.Settings.Default and can update, reload or save the appropriate setting happily, however I am getting different values when trying to load the property from a different win form that setting was saved in. For example, the following (generic names used) Properties.Settings.Default.Setting1 = "default"; in winform1 I save a string: Properties.Settings.Default.Setting1 = "string"; Then I save it. When reopening the winform I use the reload method. I then correctly see string instead of default, in the example. The setting was successfully updated. However if I try to view this setting outside of winform1, say winform2, I only see the default setting applied from inside VS. Anyone have an idea why this could be happening or directions to fix?
2 replies
CC#
Created by Curious on 3/28/2023 in #help
✅ Win Form Event Handlers question
Hi, I was just wondering if there is an event handler when the win form is idle that I can then run code in. I am looking to use it as a modeless form alongside the Revit API to sync a 2D drawing and a 3D model view together. I am currently using the Revit API Idle event, however I want to change this method of functionality to be toggled via a button in a win form as it adds unnecessary load to the Revit application. Once alternative method I thought about was running on a mouse move event, or if there is some kind of user input.
5 replies
CC#
Created by Curious on 1/11/2023 in #help
❔ different variable type depending on condition of IF statement
Hi there, how would I go about having a different type on a variable with the same name, for later use? This is required for use in my code later on within the Autodesk RevitAPI
var elmFilter = null;
if (FilterSelected == 0)
{
Helpers.ElementFilterTerminals elmFilter = new Helpers.ElementFilterTerminals();
}
if (FilterSelected == 1)
{
Helpers.ElementFilterDuctwork elmFilter = new Helpers.ElementFilterDuctwork();
}

//later on...

pickedRef = sel.PickObject(ObjectType.PointOnElement, elmFilter, "Pick the elements to be updated.");
var elmFilter = null;
if (FilterSelected == 0)
{
Helpers.ElementFilterTerminals elmFilter = new Helpers.ElementFilterTerminals();
}
if (FilterSelected == 1)
{
Helpers.ElementFilterDuctwork elmFilter = new Helpers.ElementFilterDuctwork();
}

//later on...

pickedRef = sel.PickObject(ObjectType.PointOnElement, elmFilter, "Pick the elements to be updated.");
in my helpers class:
public class ElementFilterTerminals : ISelectionFilter
{
public bool AllowElement(Element e)
{
//TO-DO: Add Magicad Family for end caps to this.
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctTerminal)) ||
(e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_MechanicalEquipment));
}
public bool AllowReference(Autodesk.Revit.DB.Reference r, XYZ p)
{

return (r.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE);
}
}

public class ElementFilterDuctwork : ISelectionFilter
{
public bool AllowElement(Element e)
{
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctSystem)) ||
(e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctFitting));
}
public bool AllowReference(Autodesk.Revit.DB.Reference r, XYZ p)
{

return (r.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE);
}
}
public class ElementFilterTerminals : ISelectionFilter
{
public bool AllowElement(Element e)
{
//TO-DO: Add Magicad Family for end caps to this.
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctTerminal)) ||
(e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_MechanicalEquipment));
}
public bool AllowReference(Autodesk.Revit.DB.Reference r, XYZ p)
{

return (r.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE);
}
}

public class ElementFilterDuctwork : ISelectionFilter
{
public bool AllowElement(Element e)
{
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctSystem)) ||
(e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctFitting));
}
public bool AllowReference(Autodesk.Revit.DB.Reference r, XYZ p)
{

return (r.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE);
}
}
12 replies
CC#
Created by Curious on 12/23/2022 in #help
❔ How to convert from one type to a different type
6 replies
CC#
Created by Curious on 12/4/2022 in #help
❔ ✅ remove from list (RevitAPI)
I have started learning C# today. Please bare with me 🙂 I am trying to remove an element from an list if its existing, otherwise add it.
pickedRef = sel.PickObject(ObjectType.PointOnElement, elmFilter, "Pick the elements to be updated.");

// create list and add/remove depending on action with element
List<ElementId> selectId = new List<ElementId>();
Color overrideColor = new Color(255, 182, 193);
ElementId elmId = pickedRef.ElementId;

if (selectId.Contains(elmId))
{
OverrideColour(uiApp, elmId, overrideColor, true);
selectId.Remove(elmId);
TaskDialog.Show("AA", "T");
}
else
{
OverrideColour(uiApp, elmId, overrideColor, false);
selectId.Add(elmId);
TaskDialog.Show("AA", "F");
}
pickedRef = sel.PickObject(ObjectType.PointOnElement, elmFilter, "Pick the elements to be updated.");

// create list and add/remove depending on action with element
List<ElementId> selectId = new List<ElementId>();
Color overrideColor = new Color(255, 182, 193);
ElementId elmId = pickedRef.ElementId;

if (selectId.Contains(elmId))
{
OverrideColour(uiApp, elmId, overrideColor, true);
selectId.Remove(elmId);
TaskDialog.Show("AA", "T");
}
else
{
OverrideColour(uiApp, elmId, overrideColor, false);
selectId.Add(elmId);
TaskDialog.Show("AA", "F");
}
I seem too only be able to run whats in the else, and never if the list contains the element. The logic works, as I have it in python already:
ref = uidoc.Selection.PickObject(ObjectType.Element, 'Pick elements in the desired order (re-select to Remove), hit ESC to stop picking.')
e_id = ref.ElementId
if e_id not in selectId:
overridecolor(e_id)
selectId.append(e_id)
else:
overridecolor(e_id, True)
selectId.pop(selectId.index(e_id))
ref = uidoc.Selection.PickObject(ObjectType.Element, 'Pick elements in the desired order (re-select to Remove), hit ESC to stop picking.')
e_id = ref.ElementId
if e_id not in selectId:
overridecolor(e_id)
selectId.append(e_id)
else:
overridecolor(e_id, True)
selectId.pop(selectId.index(e_id))
26 replies