Hi there!
I was kind of forced to re-write my python code with C# since I failed to run non-modal WPF window with xaml file. If it’s modal - everything works fine, but since I need user to make changes in Revit while script is running - I need non-modal window. I tried everything that I could find - EventHandler mostly, but just couldn’t make it work. Revit was crushing every time when I tried to look up some values, or even open PyRevit console by printing something.
So I decided to re-write everything with C# in order to achieve more control over EventHandlers. Pretty new to C#, but I know a thing or two. 80% of code was piece of cake, but then it was time for xaml and wpf handling… Yup. I can Load xaml file, and I can start wpf Window with it. But I just can’t provide data to my DataGrid since I can’t achieve access to this component.
With Python it was kinda straight forward - self.MyDataGrid… and so on.
With C# I just can’t access anything. Maybe (probably) it’s my lack of knowledge - what type of classes and functions to use in C#. Below I will share simplified version of my code, and just hope that someone can help me with this.
And yes - I know that proper way to do things is to create full-blown C# project in Visual Studio, with code-behind etc, but I really need this script to be part of my PyRevit extention. Any help will be highly appreciated!
using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Markup;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.DB.Events;
using System.Windows.Data;
namespace Checklist
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
// Here I am getting some data from CSV file and putting it to list of Class objects - dataList
// Here I will call Window.Show() from WpfInit Class, passing dataList to it
return Result.Succeeded;
}
}
public partial class WpfInit
{
public static void init(List<RowItem> dataList)
{
try
{
// here I am putting data to ListCollectionView to be able to group it
ListCollectionView finalList = new ListCollectionView(dataList);
PropertyGroupDescription groupList = new PropertyGroupDescription("type");
finalList.GroupDescriptions.Add(groupList);
FileStream xamlFile = new FileStream("ui.xaml", FileMode.Open, FileAccess.Read);
Window win = XamlReader.Load(xamlFile) as Window;
win.Show();
//self.MyDataGrid.ItemsSource = finalList <-- string from Python code, and I need to access DataGrid somehow
}
catch (Exception err)
{
TaskDialog.Show("Error", err.Message);
}
}
}
}
Hi! Can you please share more info? Maybe what kind of eror message did you get.
I noticed that in case with C# pyRevit won’t read XAML file right from the script’s directory, but rather from current working Revit directory (something like this, mine was looking for xaml in last saving directory). I guess it’s a must to put a full proper file path to xaml in the script
I did it. I will also tell you for others, if you have the same problems.
First you need to compile your code into a .dll and place it in the bin folder.
In the panel folder create a .invokebutton folder (NOT a .pushbutton) and a bundle.yamle file and just point assembly there
title: Test Direct Invoke (Custom Title)
tooltip: Test Direct Invoke Tooltip
context: zero-doc
author: "{{author}}"
highlight: new
assembly: ND_018_ParameterFuellen.pushbutton.dll
command_class: