Using pyrevit and IronPython, I've created a user control called IntegerUpDown.py, which I want to use in my main script Grids_script.py. However, I'm encountering the folowing error that I don't know how to resolve:
IronPython Traceback:
Traceback (most recent call last):
File "C:\Users\nono\AppData\Roaming\pyRevit\WPF.extension\WPF.tab\UC.panel\Grids.pushbutton\Grids_script.py", line 27, in <module>
File "C:\Users\nono\AppData\Roaming\pyRevit\WPF.extension\WPF.tab\UC.panel\Grids.pushbutton\Grids_script.py", line 12, in _init_
Exception: Impossible de créer le type inconnu '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}IntegerUpDown'.
IntegerUpDown.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinWidth="50">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<TextBox x:Name="NumericText" Grid.Column="0"
Text="0"
TextAlignment="Center"
BorderBrush="Black" Margin="0,0,0.2,0" />
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<RepeatButton x:Name="PART_IncreaseButton" Click="btnIncrease_Click" Grid.Row="0" Margin="0,0,0,0.1"
Width="15" BorderThickness="0.75">
<Polygon x:Name="PART_Up_Polygon"
HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="2"
StrokeThickness="0.5" Stroke="Transparent"
Points="0,0 -2,5 2,5" Stretch="Fill" Fill="Black"/>
</RepeatButton>
<RepeatButton x:Name="PART_DecreaseButton" Click="btndecrease_Click" Grid.Row="1" Margin="0,0.1,0,0"
Width="15" BorderThickness="0.75">
<Polygon x:Name="PART_Down_Polygon" HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="2"
StrokeThickness="0.5" Stroke="Transparent"
Points="-2,0 2,0 0,5 " Stretch="Fill" Fill="Black"/>
</RepeatButton>
</Grid>
</Grid>
</UserControl>
IntegerUpDown.py
# -*- coding: UTF-8 -*-
import wpf, os, clr
from System.Windows import Window
from System.Windows.Controls import UserControl
script_path = os.path.dirname(__file__)
class IntegerUpDown(UserControl):
def __init__(self):
xaml_path = os.path.join(script_path, 'IntegerUpDown.xaml')
wpf.LoadComponent(self, xaml_path)
def btnIncrease_Click(self, sender, e):
num = int(self.NumericText.Text)
num += 1
self.NumericText.Text = str(num)
def btnDecrease_Click(self, sender, e):
"""num = int(self.NumericText.Text)
num = max(num - 1, 0) # Ensure the value does not go below 0
self.NumericText.Text = str(num)"""
num = int(self.NumericText.Text)
num -= 1
if num > -1:
self.NumericText.Text = str(num)
else:
self.NumericText.Text = str(0)
grids.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Grids"
Height="500" Width="340"
WindowStartupLocation="CenterScreen">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="7*"/>
<RowDefinition Height="86*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<DockPanel >
<Label Content="Nom :" />
<TextBlock Text="Grid for Revit" Width="150" Margin="0,4,0,0" />
</DockPanel>
<TabControl Grid.Row="1">
<TabItem Header="X" Width="50">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65*"/>
<ColumnDefinition Width="35*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="7*"/>
<RowDefinition Height="7*"/>
<RowDefinition Height="79*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<DockPanel Grid.ColumnSpan="2">
<Label Content="Position:" Margin="0,0,40,0"/>
<Label Content="Repeter x:" Margin="0,0,40,0"/>
<Label Content="Espacement:"/>
</DockPanel >
<DockPanel Grid.Row="1" Grid.ColumnSpan="2">
<TextBox Width="50" Text="0.00" Margin="0,0,5,0"/>
<TextBlock Text="(m)" Margin="0,0,25,0" Width="20"/>
<IntegerUpDown Margin="0,0,50,0"/>
<TextBox Width="50" Text="0.00" Margin="0,0,5,0"/>
<TextBlock Text="(m)" Width="20" HorizontalAlignment="Left" />
</DockPanel>
<ListBox Grid.Row="2" Margin="0,5,0,0">
<ListBoxItem>
1
</ListBoxItem>
<ListBoxItem>
2
</ListBoxItem>
<ListBoxItem>
3
</ListBoxItem>
</ListBox>
<StackPanel Grid.Column="1" Grid.Row="2">
<Button Name="Ajouter_X" Content="Ajouter" Click="Ajouter_X_Click" VerticalAlignment="Top" Margin="5,50,5,10"/>
<Button Name="Supprimer_X" Content="Supprimer" Click="Supprimer_X_Click" Margin="5,0,5,10"/>
<Button Name="Supprimer_tout_X" Content="Supprimer tout" Click="Supprimer_tout_X_Click" Margin="5,0,5,10"/>
</StackPanel>
</Grid>
</TabControl>
<DockPanel Grid.Row="2">
<Button Content="Appliquer" Width="100" Height="25"/>
<Button Content="Fermer" Width="100" Height="25" HorizontalAlignment="Right"/>
</DockPanel>
</Grid>
</Window>
Grids_script.py
# -*- coding: UTF-8 -*-
import wpf, clr, os, sys
sys.path.append(r"D:\Examples_pyRevit\WPF_Forms")
from System.Windows import Window
from IntegerUpDown import IntegerUpDown
script_path = os.path.dirname(__file__)
class Mywindow(Window):
def __init__(self):
xaml_path = os.path.join(script_path, 'grids.xaml')
wpf.LoadComponent(self, xaml_path)
def Ajouter_X_Click(self, sender, e):
pass
def Ajouter_X_Click(self, sender, e):
pass
def Supprimer_X_Click(self, sender, e):
pass
def Supprimer_tout_X_Click(self, sender, e):
pass
if __name__ == "__main__":
Grids = Mywindow()
Grids.ShowDialog()