UI.PreviewControl Winforms


I want to display view on winform? Can you help me

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import *

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

import System

from System.Windows.Forms import *
from System.Drawing import *

from System.Collections.Generic import *

import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

preview = PreviewControl(doc,doc.ActiveView.Id)

class MainForm(Form):
def __init__(self):
self.InitializeComponent()

def PreviewHost(self):
self = System.Windows.Forms.Integration.ElementHost

def InitializeComponent(self):
self._button1 = System.Windows.Forms.Button()
self.SuspendLayout()
#
# button1
#
self._button1.Location = System.Drawing.Point(291, 408)
self._button1.Name = "button1"
self._button1.Size = System.Drawing.Size(75, 23)
self._button1.TabIndex = 0
self._button1.Text = "button1"
self._button1.UseVisualStyleBackColor = True
self._button1.Click += self.Button1Click
#
# MainForm
#
self.ClientSize = System.Drawing.Size(391, 457)
self.Controls.Add(self._button1)
self.Name = "MainForm"
self.Text = "preview"
self.ResumeLayout(False)

def Button1Click(self, sender, e):
pass

form = MainForm()
form.PreviewHost.Child = preview

form.ShowDialog();

OUT = preview

Depending on what you are after doing with a winform, it may be best to have a look at the datashapes package because this will create winforms depending on your given inputs. just have a look at the below blog posts for how it can work.

Blog post: https://data-shapes.io/blog/

image
i want preview view put into UI winform
Data-shapes slow down the process

What sort of errors are you getting in dynamo when you run your code?

If you look on the revit api for the preview control it states the following that should help you make the correct form type - “The dialog or form or window host this preview control must be modal. The view can be any graphical view but not a non-graphical view. And only one can be active. The view can be manipulated by embedded view cube and the visibility and graphical settings set on the view will be evident in the preview control.”

Also you could look at the data shapes python code for the UI’s to help understand what is happening. Then it should help you adjust yours accordingly?

@pvhuyg Could you re-edit the code you posted please. There is a problem with the quotes everywhere.

image

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

clr.AddReference("RevitAPIUI")
from  Autodesk.Revit.UI import *

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

import System

from System.Windows.Forms import *
from System.Drawing import *

from System.Collections.Generic import *

preview = PreviewControl(doc,doc.ActiveView.Id)
				
class MainForm(Form):
	def __init__(self):
	self.InitializeComponent()
	self.PreviewHost()

def PreviewHost(self):
	self.PreviewHost = System.Windows.Forms.Integration.ElementHost
def InitializeComponent(self):
	self._button1 = System.Windows.Forms.Button()
	self._panel1 = System.Windows.Forms.Panel()
	self.SuspendLayout()
	# 
	# button1
	# 
	self._button1.Location = System.Drawing.Point(291, 408)
	self._button1.Name = "button1"
	self._button1.Size = System.Drawing.Size(75, 23)
	self._button1.TabIndex = 0
	self._button1.Text = "button1"
	self._button1.UseVisualStyleBackColor = True
	self._button1.Click += self.Button1Click
	# 
	# panel1
	# 
	self._panel1.Location = System.Drawing.Point(25, 24)
	self._panel1.Name = "panel1"
	self._panel1.Size = System.Drawing.Size(341, 365)
	self._panel1.TabIndex = 1
	# 
	# MainForm
	# 
	self.ClientSize = System.Drawing.Size(391, 457)
	self.Controls.Add(self._button1)
	self.Controls.Add(self._panel1)
	self.Name = "MainForm"
	self.Text = "preview"
	self.ResumeLayout(False)

def Button1Click(self, sender, e):
	self.PreviewHost.Child = preview
	pass

form = MainForm()
form.PreviewHost.Child = preview
form.ShowDialog()

OUT = preview

sory. I fixed it. I want to get Integration from System.Windows.Forms but failed