I am looking for an example/help for displaying a gstreamer-sharp feed in a WinForms application.
I am using VS 2012 and have the "glue" project built for this version of VS. I also have glib-sharp, gstreamer-sharp installed and referenced by my project. I have the gstreamer bin directory set as my project's working directory.
If have the following code in a button click handler I get the GStreamer D3D video sink test Window which pops-up on top of my Form.
Gst.Application.Init();
var pipeline = Parse.Launch(#"videotestsrc ! videoconvert ! autovideosink");
pipeline.SetState(State.Playing);
I would like to get the stream displaying within my application on a Panel or PictureBox I'm thinking.
Thanks!
The code below will get the test stream to display on a WinForm's panel control.
internal enum videosinktype { glimagesink, d3dvideosink, dshowvideosink, directdrawsink}
static Element mVideoTestSource, mVideoCaps, mVideoSink, mAppQueue, mVideoConv;
static Gst.App.AppSink mAppSink;
static System.Threading.Thread mMainGlibThread;
static GLib.MainLoop mMainLoop; // GLib's Main Loop
private const videosinktype mCfgVideosinkType = videosinktype.dshowvideosink;
private ulong mHandle;
private Gst.Video.VideoSink mGlImageSink;
private Gst.Pipeline mCurrentPipeline = null;
private void InitGStreamerPipeline()
{
//Assign Handle to prevent Cross-Threading Access
mHandle = (ulong)videoDisplay.Handle;
//Init Gstreamer
Gst.Application.Init();
GtkSharp.GstreamerSharp.ObjectManager.Initialize();
mMainLoop = new GLib.MainLoop();
mMainGlibThread = new System.Threading.Thread(mMainLoop.Run);
mMainGlibThread.Start();
#region BuildPipeline
switch (mCfgVideosinkType)
{
case videosinktype.glimagesink:
mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("glimagesink", "glimagesink");
break;
case videosinktype.d3dvideosink:
mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("d3dvideosink", "d3dvideosink");
//mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("dshowvideosink", "dshowvideosink");
break;
case videosinktype.dshowvideosink:
mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("dshowvideosink", "dshowvideosink");
break;
case videosinktype.directdrawsink:
mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("directdrawsink", "directdrawsink");
break;
default:
break;
}
mVideoTestSource = ElementFactory.Make("videotestsrc", "videotestsrc0");
mVideoConv = ElementFactory.Make("videoconvert", "vidconvert0");
mVideoSink = ElementFactory.Make("autovideosink", "sink0");
mCurrentPipeline = new Gst.Pipeline("pipeline");
mCurrentPipeline.Add(mVideoTestSource, mVideoConv, mVideoSink, mGlImageSink);
if (!mVideoTestSource.Link(mVideoSink))
if (mUdpcSrc.Link(mVideoSink))
{
System.Diagnostics.Debug.WriteLine("Elements could not be linked");
}
#endregion
//subscribe to bus & bussync msgs
Bus bus = mCurrentPipeline.Bus;
bus.AddSignalWatch();
bus.Message += HandleMessage;
Bus bus = mCurrentPipeline.Bus;
bus.EnableSyncMessageEmission();
bus.SyncMessage += new SyncMessageHandler(bus_SyncMessage);
//play the stream
var setStateRet = mCurrentPipeline.SetState(State.Null);
System.Diagnostics.Debug.WriteLine("SetStateNULL returned: " + setStateRet.ToString());
setStateRet = mCurrentPipeline.SetState(State.Ready);
System.Diagnostics.Debug.WriteLine("SetStateReady returned: " + setStateRet.ToString());
setStateRet = mCurrentPipeline.SetState(Gst.State.Playing);
}
/// <summary>
///
/// </summary>
/// <remarks>
/// Indeed the application needs to set its Window identifier at the right time to avoid internal Window creation
/// from the video sink element. To solve this issue a GstMessage is posted on the bus to inform the application
/// that it should set the Window identifier immediately.
///
/// API: http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-libs/html/gst-plugins-base-libs-gstvideooverlay.html
/// </remarks>
/// <param name="o"></param>
/// <param name="args"></param>
private void bus_SyncMessage(object o, SyncMessageArgs args)
{
//Convenience function to check if the given message is a "prepare-window-handle" message from a GstVideoOverlay.
System.Diagnostics.Debug.WriteLine("bus_SyncMessage: " + args.Message.Type.ToString());
if (Gst.Video.Global.IsVideoOverlayPrepareWindowHandleMessage(args.Message))
{
Element src = (Gst.Element)args.Message.Src;
#if DEBUG
System.Diagnostics.Debug.WriteLine("Message'prepare-window-handle' received by: " + src.Name + " " + src.ToString());
#endif
if (src != null && (src is Gst.Video.VideoSink | src is Gst.Bin))
{
// Try to set Aspect Ratio
try
{
src["force-aspect-ratio"] = true;
}
catch (PropertyNotFoundException) { }
// Try to set Overlay
try
{
Gst.Video.VideoOverlayAdapter overlay_ = new Gst.Video.VideoOverlayAdapter(src.Handle);
overlay_.WindowHandle = mHandle;
overlay_.HandleEvents(true);
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception thrown: " + ex.Message); }
}
}
}
private void HandleMessage (object o, MessageArgs args)
{
var msg = args.Message;
//System.Diagnostics.Debug.WriteLine("HandleMessage received msg of type: {0}", msg.Type);
switch (msg.Type)
{
case MessageType.Error:
//
GLib.GException err;
string debug;
System.Diagnostics.Debug.WriteLine("Error received: " + msg.ToString());
//msg.ParseError (out err, out debug);
//if(debug == null) { debug = "none"; }
//System.Diagnostics.Debug.WriteLine ("Error received from element {0}: {1}", msg.Src, err.Message);
//System.Diagnostics.Debug.WriteLine ("Debugging information: "+ debug);
break;
case MessageType.StreamStatus:
Gst.StreamStatusType status;
Element theOwner;
msg.ParseStreamStatus(out status, out theOwner);
System.Diagnostics.Debug.WriteLine("Case SteamingStatus: status is: " + status + " ; Ownder is: " + theOwner.Name);
break;
case MessageType.StateChanged:
//System.Diagnostics.Debug.WriteLine("Case StateChanged: " + args.Message.ToString());
State oldState, newState, pendingState;
msg.ParseStateChanged(out oldState, out newState, out pendingState);
if (newState == State.Paused)
args.RetVal = false;
System.Diagnostics.Debug.WriteLine("Pipeline state changed from {0} to {1}: ; Pending: {2}", Element.StateGetName(oldState), Element.StateGetName(newState), Element.StateGetName(pendingState));
break;
case MessageType.Element:
System.Diagnostics.Debug.WriteLine("Element message: {0}", args.Message.ToString());
break;
default:
System.Diagnostics.Debug.WriteLine("HandleMessage received msg of type: {0}", msg.Type);
break;
}
args.RetVal = true;
}
Have a look at the GstVideoOverlay interface here:
https://developer.gnome.org/gst-plugins-libs/stable/gst-plugins-base-libs-gstvideooverlay.html
You will have to adapt to the C# syntax I guess, but there is a good amount of explaination and code samples for this.
You can have a look at https://cgit.freedesktop.org/gstreamer/gstreamer-sharp/tree/samples/VideoOverlay.cs You need to adapt this code to use the window Handle of WinForms.
Related
So i modified a sample code from here https://code.msdn.microsoft.com/windowsdesktop/File-Synchronization-516e3ad7 a little to fit my needs. This is a console application that takes in 2 arguments, first is the source, second is destination. It's a one way sync source to destination folders.
// <copyright file="SyncMain.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>Sample code for using the MSF File Sync Provider</summary>
using System;
using System.IO;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Files;
public class FileSyncProviderSample
{
public static void Main(string[] args)
{
if (args.Length < 2 ||
string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]) ||
!Directory.Exists(args[0]) || !Directory.Exists(args[1]))
{
Console.WriteLine(
"Usage: FileSyncSample [valid directory path 1] [valid directory path 2]");
return;
}
string replica1RootPath = args[0];
string replica2RootPath = args[1];
try
{
// Set options for the sync operation
FileSyncOptions options =
FileSyncOptions.ExplicitDetectChanges |
FileSyncOptions.RecycleDeletedFiles |
FileSyncOptions.RecyclePreviousFileOnUpdates |
FileSyncOptions.RecycleConflictLoserFiles;
FileSyncScopeFilter filter = new FileSyncScopeFilter();
filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files
// Explicitly detect changes on both replicas upfront, to avoid two change
// detection passes for the two-way sync
DetectChangesOnFileSystemReplica(
replica1RootPath, filter, options);
DetectChangesOnFileSystemReplica(
replica2RootPath, filter, options);
// Sync in both directions
SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options);
//SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options);
}
catch (Exception e)
{
Console.WriteLine("\nException from File Sync Provider:\n" + e.ToString());
}
}
public static void DetectChangesOnFileSystemReplica(
string replicaRootPath,
FileSyncScopeFilter filter, FileSyncOptions options)
{
FileSyncProvider provider = null;
try
{
provider = new FileSyncProvider(replicaRootPath, filter, options);
provider.DetectChanges();
}
finally
{
// Release resources
if (provider != null)
provider.Dispose();
}
}
public static void SyncFileSystemReplicasOneWay(
string sourceReplicaRootPath, string destinationReplicaRootPath,
FileSyncScopeFilter filter, FileSyncOptions options)
{
FileSyncProvider sourceProvider = null;
FileSyncProvider destinationProvider = null;
try
{
sourceProvider = new FileSyncProvider(
sourceReplicaRootPath, filter, options);
destinationProvider = new FileSyncProvider(
destinationReplicaRootPath, filter, options);
destinationProvider.AppliedChange +=
new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
destinationProvider.SkippedChange +=
new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);
SyncOrchestrator agent = new SyncOrchestrator();
agent.LocalProvider = sourceProvider;
agent.RemoteProvider = destinationProvider;
agent.Direction = SyncDirectionOrder.Upload; // Sync source to destination
Console.WriteLine("Synchronizing changes to replica: " +
destinationProvider.RootDirectoryPath);
agent.Synchronize();
}
finally
{
// Release resources
if (sourceProvider != null) sourceProvider.Dispose();
if (destinationProvider != null) destinationProvider.Dispose();
}
}
public static void OnAppliedChange(object sender, AppliedChangeEventArgs args)
{
switch(args.ChangeType)
{
case ChangeType.Create:
Console.WriteLine("-- Applied CREATE for file " + args.NewFilePath);
break;
case ChangeType.Delete:
Console.WriteLine("-- Applied DELETE for file " + args.OldFilePath);
break;
case ChangeType.Update:
Console.WriteLine("-- Applied OVERWRITE for file " + args.OldFilePath);
break;
case ChangeType.Rename:
Console.WriteLine("-- Applied RENAME for file " + args.OldFilePath +
" as " + args.NewFilePath);
break;
}
}
public static void OnSkippedChange(object sender, SkippedChangeEventArgs args)
{
Console.WriteLine("-- Skipped applying " + args.ChangeType.ToString().ToUpper()
+ " for " + (!string.IsNullOrEmpty(args.CurrentFilePath) ?
args.CurrentFilePath : args.NewFilePath) + " due to error");
if (args.Exception != null)
Console.WriteLine(" [" + args.Exception.Message + "]");
}
}
It works just as expected, but when i modified the code and uses it in Unity, it crashes in editor playmode and does nothing when running the build. The crash seems to originate from agent.Synchronize();
Script in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Files;
using System;
using System.IO;
public class FileSyncTest : MonoBehaviour
{
[SerializeField] private string replica1RootPath;
[SerializeField] private string replica2RootPath;
[SerializeField] private KeyCode activateSyncKey = KeyCode.Keypad5;
private void Update()
{
if (Input.GetKeyDown(activateSyncKey))
{
if (Directory.Exists(replica1RootPath) || Directory.Exists(replica2RootPath))
{
try
{
// Set options for the sync operation
FileSyncOptions options =
FileSyncOptions.ExplicitDetectChanges |
FileSyncOptions.RecycleDeletedFiles |
FileSyncOptions.RecyclePreviousFileOnUpdates |
FileSyncOptions.RecycleConflictLoserFiles;
FileSyncScopeFilter filter = new FileSyncScopeFilter();
filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files
// Explicitly detect changes on both replicas upfront, to avoid two change
// detection passes for the two-way sync
DetectChangesOnFileSystemReplica(
replica1RootPath, filter, options);
DetectChangesOnFileSystemReplica(
replica2RootPath, filter, options);
// Sync in both directions
SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options);
//SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options);
}
catch (Exception e)
{
Debug.Log("\nException from File Sync Provider:\n" + e.ToString());
}
}
else
{
Debug.Log("One of the dir doesn't exist");
}
}
}
public void DetectChangesOnFileSystemReplica(
string replicaRootPath,
FileSyncScopeFilter filter, FileSyncOptions options)
{
FileSyncProvider provider = null;
try
{
provider = new FileSyncProvider(replicaRootPath, filter, options);
provider.DetectChanges();
}
finally
{
// Release resources
if (provider != null)
provider.Dispose();
}
}
private void SyncFileSystemReplicasOneWay(
string sourceReplicaRootPath, string destinationReplicaRootPath,
FileSyncScopeFilter filter, FileSyncOptions options)
{
FileSyncProvider sourceProvider = null;
FileSyncProvider destinationProvider = null;
try
{
sourceProvider = new FileSyncProvider(
sourceReplicaRootPath, filter, options);
destinationProvider = new FileSyncProvider(
destinationReplicaRootPath, filter, options);
destinationProvider.AppliedChange +=
new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
destinationProvider.SkippedChange +=
new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);
SyncOrchestrator agent = new SyncOrchestrator();
agent.LocalProvider = sourceProvider;
agent.RemoteProvider = destinationProvider;
agent.Direction = SyncDirectionOrder.Upload; // Sync source to destination
Debug.Log("Synchronizing changes to replica: " +
destinationProvider.RootDirectoryPath);
agent.Synchronize();
}
finally
{
// Release resources
if (sourceProvider != null) sourceProvider.Dispose();
if (destinationProvider != null) destinationProvider.Dispose();
}
}
private void OnAppliedChange(object sender, AppliedChangeEventArgs args)
{
switch (args.ChangeType)
{
case ChangeType.Create:
Debug.Log("-- Applied CREATE for file " + args.NewFilePath);
break;
case ChangeType.Delete:
Debug.Log("-- Applied DELETE for file " + args.OldFilePath);
break;
case ChangeType.Update:
Debug.Log("-- Applied OVERWRITE for file " + args.OldFilePath);
break;
case ChangeType.Rename:
Debug.Log("-- Applied RENAME for file " + args.OldFilePath +
" as " + args.NewFilePath);
break;
}
}
private void OnSkippedChange(object sender, SkippedChangeEventArgs args)
{
Debug.Log("-- Skipped applying " + args.ChangeType.ToString().ToUpper()
+ " for " + (!string.IsNullOrEmpty(args.CurrentFilePath) ?
args.CurrentFilePath : args.NewFilePath) + " due to error");
if (args.Exception != null)
Debug.Log(" [" + args.Exception.Message + "]");
}
}
I have copied the required DLLs into the project folders, below are the DLLs i copied.
FeedSync.21.dll
FileSyncProvider21.dll
MetaStore21.dll
Microsoft.Synchronization.dll
Microsoft.Synchronization.Files.dll
Microsoft.Synchronization.MetadataStorage.dll
Microsoft.Synchronization.SimpleProviders.dll
Msfdb.dll
Msfdbqp.dll
Msfdbse.dll
-SimpleProviders21.dll
-Synchronization21.dll
Editor.log
- https://pastebin.com/3MnNFY4z
Any help will be appreciated!
I am wondering how/if it's possible to use the follwing gstreamer bindings using gstreamer-sharp/C# for Windows?
udpsrc port=1234 ! application/x-rtp, payload=127 ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink
I am not sure how to do the "application/x-rtp, payload=127" part and I think that's all I am missing, but I am not 100% sure about this. The videosink I am trying to use is dshowvideosink.
Here is relavent parts of my code so far. The variable videoDisplay is just a WinForms Panel control on my form.
internal enum videosinktype { glimagesink, d3dvideosink, dshowvideosink, directdrawsink}
static Element mVideoConv, mUdpcSrc, mDemux, mAvDecH264, mAVSink;
static Gst.App.AppSink mAppSink;
static System.Threading.Thread mMainGlibThread;
static GLib.MainLoop mMainLoop; // GLib's Main Loop
private const videosinktype mCfgVideosinkType = videosinktype.dshowvideosink;
private ulong mHandle;
private Gst.Video.VideoSink mGlImageSink;
private Gst.Pipeline mCurrentPipeline = null;
private void InitGStreamerPipeline()
{
//Assign Handle to prevent Cross-Threading Access
mHandle = (ulong)videoDisplay.Handle;
//Init Gstreamer
Gst.Application.Init();
GtkSharp.GstreamerSharp.ObjectManager.Initialize();
mMainLoop = new GLib.MainLoop();
mMainGlibThread = new System.Threading.Thread(mMainLoop.Run);
mMainGlibThread.Start();
#region BuildPipeline
switch (mCfgVideosinkType)
{
case videosinktype.glimagesink:
mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("glimagesink", "glimagesink");
break;
case videosinktype.d3dvideosink:
mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("d3dvideosink", "d3dvideosink");
//mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("dshowvideosink", "dshowvideosink");
break;
case videosinktype.dshowvideosink:
mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("dshowvideosink", "dshowvideosink");
break;
case videosinktype.directdrawsink:
mGlImageSink = (Gst.Video.VideoSink)Gst.ElementFactory.Make("directdrawsink", "directdrawsink");
break;
default:
break;
}
//what's the gstreamer-sharp equivalent of:
//application/x-rtp, payload=127
//??
mUdpcSrc = ElementFactory.Make("udpsrc", "sink0");
mUdpcSrc["port"] = 1234;
mDemux = ElementFactory.Make("rtph264depay", "depay0");
mAvDecH264 = ElementFactory.Make("avdec_h264", "avdech2640");
mVideoConv = ElementFactory.Make("videoconvert", "vidconvert0");
mAVSink = ElementFactory.Make("autovideosink", "video_sink");
mCurrentPipeline = new Gst.Pipeline("pipeline");
mCurrentPipeline.Add(mUdpcSrc, mDemux, mAvDecH264, mVideoConv, mAVSink);
if (!mVideoTestSource.Link(mVideoSink))
if (mUdpcSrc.Link(mVideoSink))
{
System.Diagnostics.Debug.WriteLine("Elements could not be linked");
}
#endregion
//subscribe to bus & bussync msgs
Bus bus = mCurrentPipeline.Bus;
bus.AddSignalWatch();
bus.Message += HandleMessage;
Bus bus = mCurrentPipeline.Bus;
bus.EnableSyncMessageEmission();
bus.SyncMessage += new SyncMessageHandler(bus_SyncMessage);
//play the stream
var setStateRet = mCurrentPipeline.SetState(State.Null);
System.Diagnostics.Debug.WriteLine("SetStateNULL returned: " + setStateRet.ToString());
setStateRet = mCurrentPipeline.SetState(State.Ready);
System.Diagnostics.Debug.WriteLine("SetStateReady returned: " + setStateRet.ToString());
setStateRet = mCurrentPipeline.SetState(Gst.State.Playing);
}
/// <summary>
///
/// </summary>
/// <remarks>
/// Indeed the application needs to set its Window identifier at the right time to avoid internal Window creation
/// from the video sink element. To solve this issue a GstMessage is posted on the bus to inform the application
/// that it should set the Window identifier immediately.
///
/// API: http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-libs/html/gst-plugins-base-libs-gstvideooverlay.html
/// </remarks>
/// <param name="o"></param>
/// <param name="args"></param>
private void bus_SyncMessage(object o, SyncMessageArgs args)
{
//Convenience function to check if the given message is a "prepare-window-handle" message from a GstVideoOverlay.
System.Diagnostics.Debug.WriteLine("bus_SyncMessage: " + args.Message.Type.ToString());
if (Gst.Video.Global.IsVideoOverlayPrepareWindowHandleMessage(args.Message))
{
Element src = (Gst.Element)args.Message.Src;
#if DEBUG
System.Diagnostics.Debug.WriteLine("Message'prepare-window-handle' received by: " + src.Name + " " + src.ToString());
#endif
if (src != null && (src is Gst.Video.VideoSink | src is Gst.Bin))
{
// Try to set Aspect Ratio
try
{
src["force-aspect-ratio"] = true;
}
catch (PropertyNotFoundException) { }
// Try to set Overlay
try
{
Gst.Video.VideoOverlayAdapter overlay_ = new Gst.Video.VideoOverlayAdapter(src.Handle);
overlay_.WindowHandle = mHandle;
overlay_.HandleEvents(true);
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception thrown: " + ex.Message); }
}
}
}
private void HandleMessage (object o, MessageArgs args)
{
var msg = args.Message;
//System.Diagnostics.Debug.WriteLine("HandleMessage received msg of type: {0}", msg.Type);
switch (msg.Type)
{
case MessageType.Error:
//
GLib.GException err;
string debug;
System.Diagnostics.Debug.WriteLine("Error received: " + msg.ToString());
//msg.ParseError (out err, out debug);
//if(debug == null) { debug = "none"; }
//System.Diagnostics.Debug.WriteLine ("Error received from element {0}: {1}", msg.Src, err.Message);
//System.Diagnostics.Debug.WriteLine ("Debugging information: "+ debug);
break;
case MessageType.StreamStatus:
Gst.StreamStatusType status;
Element theOwner;
msg.ParseStreamStatus(out status, out theOwner);
System.Diagnostics.Debug.WriteLine("Case SteamingStatus: status is: " + status + " ; Ownder is: " + theOwner.Name);
break;
case MessageType.StateChanged:
//System.Diagnostics.Debug.WriteLine("Case StateChanged: " + args.Message.ToString());
State oldState, newState, pendingState;
msg.ParseStateChanged(out oldState, out newState, out pendingState);
if (newState == State.Paused)
args.RetVal = false;
System.Diagnostics.Debug.WriteLine("Pipeline state changed from {0} to {1}: ; Pending: {2}", Element.StateGetName(oldState), Element.StateGetName(newState), Element.StateGetName(pendingState));
break;
case MessageType.Element:
System.Diagnostics.Debug.WriteLine("Element message: {0}", args.Message.ToString());
break;
default:
System.Diagnostics.Debug.WriteLine("HandleMessage received msg of type: {0}", msg.Type);
break;
}
args.RetVal = true;
}
application/x-rtp, payload=127 is just the GstCaps for the udpsrc element. You can listen on the pad-added signal and add the caps once a pad is created.
new Caps("application/x-rtp, payload=(int)127");
I'm very new to C# and coding. If possible I'm after some assistance figuring out how to fix this piece of code up to work.
They work individually. I can create a new button on the ribbon and execute the standard hello world. i also have a macro which works where I can remove all my sheets and views successfully but trying to combine the two is causing big difficulties! The code builds OK but I'm getting this error inside revit:
'Failed to initialize the add-in "Delete Views" because the class
"DeleteViews" cannot be found in the add-in assembly.The FullClassName
provides the enrty point for Revit to call add-in application. For
Revit to run the add-in, you must ensure this class implements the
"Autodesk.Revit.UI.ExternalCommand" interface.'
I'm pretty sure my problem is referencing in this 2nd bit of code. i know I'm not calling it up correctly but haven't been able to find any solutions.
Apologies if this is a dumb question but would really appreciate any help to help me learn!
Thanks for any help you can give me
The code:
namespace BGPanel
{
public class CsBGPanel : IExternalApplication
{
public UIDocument ActiveUIDocument { get; private set; }
public Result OnStartup(UIControlledApplication application)
{
RibbonPanel ribbonPanel = application.CreateRibbonPanel("Tools");
string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
PushButtonData buttonData = new PushButtonData("cmdDeleteViews",
"Delete Views", thisAssemblyPath, "BGPanel.DeleteViews");
PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;
pushButton.ToolTip = "Delete all sheets, schedules & views except structural plans";
Uri uriImage = new Uri(#"C:\Revit_API\Revit_2015\32px-Broom.png");
BitmapImage largeImage = new BitmapImage(uriImage);
pushButton.LargeImage = largeImage;
return Result.Succeeded;
}
public void DeleteViews()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();
using (Transaction t = new Transaction(doc, "Delete Views"))
{
t.Start();
int x = 0;
foreach (Element e in collection)
{
try
{
View view = e as View;
switch (view.ViewType)
{
case ViewType.FloorPlan:
break;
case ViewType.EngineeringPlan:
break;
case ViewType.ThreeD:
break;
default:
doc.Delete(e.Id);
x += 1;
break;
}
}
catch (Exception ex)
{
View view = e as View;
TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
TaskDialog.Show("Error", ex.Message);
}
}
t.Commit();
TaskDialog.Show("BG_API DeleteViews", "Views Deleted: " + x.ToString());
}
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
}
First, instead typing the class name yourself, consider using Reflection like:
typeof(YourClassName).FullName
Second, every command in Revit requires its own class that implements IExternalCommand. I haven't tested, but your code should be something like the following:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Media.Imaging;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace BGPanel
{
public class CsBGPanel : IExternalApplication
{
public Result OnStartup(UIControlledApplication application)
{
RibbonPanel ribbonPanel = application.CreateRibbonPanel("Tools");
string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
PushButtonData buttonData = new PushButtonData("cmdDeleteViews",
"Delete Views", thisAssemblyPath, typeof(DeleteViews).FullName);
PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;
pushButton.ToolTip = "Delete all sheets, schedules & views except structural plans";
Uri uriImage = new Uri(#"C:\Revit_API\Revit_2015\32px-Broom.png");
BitmapImage largeImage = new BitmapImage(uriImage);
pushButton.LargeImage = largeImage;
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
public class DeleteViews : IExternalCommand
{
// this will not work...
//public UIDocument ActiveUIDocument { get; private set; }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument; //this.ActiveUIDocument;
Document doc = uidoc.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();
using (Transaction t = new Transaction(doc, "Delete Views"))
{
t.Start();
int x = 0;
foreach (Element e in collection)
{
try
{
View view = e as View;
switch (view.ViewType)
{
case ViewType.FloorPlan:
break;
case ViewType.EngineeringPlan:
break;
case ViewType.ThreeD:
break;
default:
doc.Delete(e.Id);
x += 1;
break;
}
}
catch (Exception ex)
{
View view = e as View;
TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
TaskDialog.Show("Error", ex.Message);
}
}
t.Commit();
TaskDialog.Show("BG_API DeleteViews", "Views Deleted: " + x.ToString());
}
return Result.Succeeded; // must return here
}
}
}
You should work through the Revit API getting started material before doing anything else at all, especially the DevTV and My First Revit Plugin video tutorials:
http://thebuildingcoder.typepad.com/blog/about-the-author.html#2
Then this question and many other fundamental issues will be answered up front, and you will save yourself and others some effort and head-scratching.
I needed to add a line before the IExternalCommand because of transaction errors when I clicked my button.
[Transaction(TransactionMode.Manual)]
The first answer to this question suggests using the reflection "typeof(YourClassName).FullName" When the sample code is tried it returns a error, understandably as Augusto says he did not test his example. Just adding for anyone down the road... If you use "typeof(DeleteViews).FullName" you would need to cast it into a string variable before the code in his example would work.
I'm trying to figure out how I can read a param. I got the hook working, only thing is that whenever I do this, it crashes:
private void onFuncCall(NktHook hook, NktProcess process, NktHookCallInfo hookCallInfo)
var paramsEnum = hookCallInfo.Params();
if (hook.FunctionName.Equals("getPlayerPtr"))
{
INktParam p;
p = paramsEnum.First();
Debug.WriteLine(p.Value);//This line cause a crash
return;
}
}
getPlayerPtr definition:
UINT64 *getPlayerPtr(int Id);
This code should print all parameters values:
private void OnFunctionCalled(NktHook hook, NktProcess process, NktHookCallInfo hookCallInfo)
{
Output(hook.FunctionName + "( ");
bool first = true;
foreach (INktParam param in hookCallInfo.Params())
{
if (first)
first = false;
else
{
Output(", ");
}
Output(param.Name + " = " + param.Value.ToString());
}
Output(" )" + Environment.NewLine);
}
It it doesn't work means that the hooked function isn't in Deviare Database. If that's the situation you should create a custom database:
http://forum.nektra.com/forum/viewtopic.php?f=9&t=7130
I'm building a .Net 4.0 application for remote control of a scanner device. I have tried both TWAIN and WIA libraries, but I have the same problem. Scanning images without scanner selection and scanning settings dialogs.
I found a useful article on WIA scripting in .Net, and modified it to this:
private Image Scan(string deviceName)
{
WiaClass wiaManager = null; // WIA manager COM object
CollectionClass wiaDevs = null; // WIA devices collection COM object
ItemClass wiaRoot = null; // WIA root device COM object
CollectionClass wiaPics = null; // WIA collection COM object
ItemClass wiaItem = null; // WIA image COM object
try
{
// create COM instance of WIA manager
wiaManager = new WiaClass();
// call Wia.Devices to get all devices
wiaDevs = wiaManager.Devices as CollectionClass;
if ((wiaDevs == null) || (wiaDevs.Count == 0))
{
throw new Exception("No WIA devices found!");
}
object device = null;
foreach (IWiaDeviceInfo currentDevice in wiaManager.Devices)
{
if (currentDevice.Name == deviceName)
{
device = currentDevice;
break;
}
}
if (device == null)
{
throw new Exception
(
"Device with name \"" +
deviceName +
"\" could not be found."
);
}
// select device
wiaRoot = (ItemClass)wiaManager.Create(ref device);
// something went wrong
if (wiaRoot == null)
{
throw new Exception
(
"Could not initialize device \"" +
deviceName + "\"."
);
}
wiaPics = wiaRoot.GetItemsFromUI
(
WiaFlag.SingleImage,
WiaIntent.ImageTypeColor
) as CollectionClass;
if (wiaPics == null || wiaPics.Count == 0)
{
throw new Exception("Could not scan image.");
}
Image image = null;
// enumerate all the pictures the user selected
foreach (object wiaObj in wiaPics)
{
if (image == null)
{
wiaItem = (ItemClass)Marshal.CreateWrapperOfType
(
wiaObj, typeof(ItemClass)
);
// create temporary file for image
string tempFile = Path.GetTempFileName();
// transfer picture to our temporary file
wiaItem.Transfer(tempFile, false);
// create Image instance from file
image = Image.FromFile(tempFile);
}
// release enumerated COM object
Marshal.ReleaseComObject(wiaObj);
}
if (image == null)
{
throw new Exception("Error reading scanned image.");
}
return image;
}
finally
{
// release WIA image COM object
if (wiaItem != null)
Marshal.ReleaseComObject(wiaItem);
// release WIA collection COM object
if (wiaPics != null)
Marshal.ReleaseComObject(wiaPics);
// release WIA root device COM object
if (wiaRoot != null)
Marshal.ReleaseComObject(wiaRoot);
// release WIA devices collection COM object
if (wiaDevs != null)
Marshal.ReleaseComObject(wiaDevs);
// release WIA manager COM object
if (wiaManager != null)
Marshal.ReleaseComObject(wiaManager);
}
}
With this I actually managed to select the device from configuration (input parameter of the Scan method) and retrieve the resulting image after scan.
But the problem with scanning options dialog (Scan using DEVICENAME). As this is a remote control application, dialog will not be visible to the user, so I need to either skip it using default settings, or use settings from a configuration if necessary.
Scanning options dialog:
In the end I did not use the code written in the question for scanning dialogs. I found a useful example of Scanning with Windows Image Acquisition 2.0 which by the way also had a blocking dialog, but this was easily modified and in moments I had a simple class with a Scan(string scannerId) function which would just scan with a selected device and nothing more, see code () below:
using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
namespace WIATest
{
class WIAScanner
{
const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
class WIA_DPS_DOCUMENT_HANDLING_SELECT
{
public const uint FEEDER = 0x00000001;
public const uint FLATBED = 0x00000002;
}
class WIA_DPS_DOCUMENT_HANDLING_STATUS
{
public const uint FEED_READY = 0x00000001;
}
class WIA_PROPERTIES
{
public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
public const uint WIA_DIP_FIRST = 2;
public const uint WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
//
// Scanner only device properties (DPS)
//
public const uint WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
}
/// <summary>
/// Use scanner to scan an image (with user selecting the scanner from a dialog).
/// </summary>
/// <returns>Scanned images.</returns>
public static List<Image> Scan()
{
WIA.ICommonDialog dialog = new WIA.CommonDialog();
WIA.Device device = dialog.ShowSelectDevice(WIA.WiaDeviceType.UnspecifiedDeviceType, true, false);
if (device != null)
{
return Scan(device.DeviceID);
}
else
{
throw new Exception("You must select a device for scanning.");
}
}
/// <summary>
/// Use scanner to scan an image (scanner is selected by its unique id).
/// </summary>
/// <param name="scannerName"></param>
/// <returns>Scanned images.</returns>
public static List<Image> Scan(string scannerId)
{
List<Image> images = new List<Image>();
bool hasMorePages = true;
while (hasMorePages)
{
// select the correct scanner using the provided scannerId parameter
WIA.DeviceManager manager = new WIA.DeviceManager();
WIA.Device device = null;
foreach (WIA.DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == scannerId)
{
// connect to scanner
device = info.Connect();
break;
}
}
// device was not found
if (device == null)
{
// enumerate available devices
string availableDevices = "";
foreach (WIA.DeviceInfo info in manager.DeviceInfos)
{
availableDevices += info.DeviceID + "n";
}
// show error with available devices
throw new Exception("The device with provided ID could not be found. Available Devices:n" + availableDevices);
}
WIA.Item item = device.Items[1] as WIA.Item;
try
{
// scan image
WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);
// save to temp file
string fileName = Path.GetTempFileName();
File.Delete(fileName);
image.SaveFile(fileName);
image = null;
// add file to output list
images.Add(Image.FromFile(fileName));
}
catch (Exception exc)
{
throw exc;
}
finally
{
item = null;
//determine if there are any more pages waiting
WIA.Property documentHandlingSelect = null;
WIA.Property documentHandlingStatus = null;
foreach (WIA.Property prop in device.Properties)
{
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
documentHandlingSelect = prop;
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
documentHandlingStatus = prop;
}
// assume there are no more pages
hasMorePages = false;
// may not exist on flatbed scanner but required for feeder
if (documentHandlingSelect != null)
{
// check for document feeder
if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) &amp;amp; WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
{
hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) &amp;amp; WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
}
}
}
}
return images;
}
/// <summary>
/// Gets the list of available WIA devices.
/// </summary>
/// <returns></returns>
public static List<string> GetDevices()
{
List<string> devices = new List<string>();
WIA.DeviceManager manager = new WIA.DeviceManager();
foreach (WIA.DeviceInfo info in manager.DeviceInfos)
{
devices.Add(info.DeviceID);
}
return devices;
}
}
}
First off, many thanks to Miljenko Barbir for his above solution, it works great.
I would like to add that if you want zero dialogs, you can use (from Milijenko's demo code)
WIA.ImageFile image = item.Transfer(wiaFormatBMP);
instead of
WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);
This basically removes the progress bar as well, so you get scanning without any dialogs.
// show scanner view
guif.ShowUI = 0;
guif.ModalUI = 0;
You can see in this code that's I've implemented.