/// <summary> /// Determine appropriate field points and values to display /// the given greyscale bitmap image data on the given face. /// </summary> static void GetFieldPointsAndValues( ref IList <UV> pts, ref IList <ValueAtPoint> valuesAtPoints, ref GreyscaleBitmapData data, Face face) { BoundingBoxUV bb = face.GetBoundingBox(); double umin = bb.Min.U; double umax = bb.Max.U; double ustep = (umax - umin) / data.Width; double u = umin; double v = bb.Min.V; double vmax = bb.Max.V; double vstep = (vmax - v) / data.Height; List <double> values = new List <double>(1); for (int y = 0; y < data.Height; ++y, v += vstep) { Debug.Assert(v < vmax, "expected v to remain within bounds"); u = umin; for (int x = 0; x < data.Width; ++x, u += ustep) { Debug.Assert(u < umax, "expected u to remain within bounds"); double brightness = data.GetBrightnessAt( x, y); UV uv = new UV(u, v); pts.Add(uv); values.Clear(); values.Add(brightness); valuesAtPoints.Add(new ValueAtPoint( values)); } } }
/// <summary> /// External webcam event driver. /// Check regularly whether the webcam image has /// been updated. If so, update the spatial field /// primitive with the new image data. /// Currently, we only display a grey scale image. /// Colour images could be handled as well by /// defining a custom colour palette. /// </summary> static void Run() { _running = true; while (_running) { _data = new GreyscaleBitmapData( _width, _height, _url); byte[] hash = _data.HashValue; if (null == _lastHash || 0 != CompareBytes(hash, _lastHash)) { _lastHash = hash; _event.Raise(); } Thread.Sleep(_intervalMs); } }
/// <summary> /// Handle Revit Idling event. /// If less time elapsed than the specified interval, return immediately. /// Otherwise, download the current image frile from the specified URL. /// If it has not changed since the last update, return immediately. /// Otherwise, update the spatial field primitive with the new image data. /// Currently, we only display a grey scale image. /// Colour images could be handled as well by defining a custom colour palette. /// </summary> static void OnIdling( object sender, IdlingEventArgs e) { if (DateTime.Now.Subtract(_lastUpdate) > _interval) { Log("OnIdling"); GreyscaleBitmapData data = new GreyscaleBitmapData( _width, _height, _url); byte[] hash = data.HashValue; if (null == _lastHash || 0 != CompareBytes(hash, _lastHash)) { _lastHash = hash; // access active document from sender: Application app = sender as Application; Debug.Assert(null != app, "expected a valid Revit application instance"); UIApplication uiapp = new UIApplication(app); UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uidoc.Document; Log("OnIdling image changed, active document " + doc.Title); Transaction transaction = new Transaction(doc, "Revit Webcam Update"); transaction.Start(); View view = doc.ActiveView; // maybe has to be 3D SpatialFieldManager sfm = SpatialFieldManager.GetSpatialFieldManager( view); if (null == sfm) { sfm = SpatialFieldManager .CreateSpatialFieldManager(view, 1); } if (0 > _sfp_index) { _sfp_index = sfm.AddSpatialFieldPrimitive( _faceReference); } int nPoints = data.Width * data.Height; IList <UV> pts = new List <UV>(nPoints); IList <ValueAtPoint> valuesAtPoints = new List <ValueAtPoint>(nPoints); // warning CS0618: // GeometryObject is obsolete: Property will be removed. // Use Element.GetGeometryObjectFromReference(Reference) instead //Face face = _faceReference.GeometryObject as Face; // 2011 Face face = doc.get_Element(_elementId) .GetGeometryObjectFromReference( _faceReference) as Face; // 2012 GetFieldPointsAndValues(ref pts, ref valuesAtPoints, ref data, face); FieldDomainPointsByUV fieldPoints = new FieldDomainPointsByUV(pts); FieldValues fieldValues = new FieldValues(valuesAtPoints); // warning CS0618: // UpdateSpatialFieldPrimitive(int, FieldDomainPoints, FieldValues) is obsolete: // This method is obsolete in Revit 2012; use the overload accepting the result index instead. //sfm.UpdateSpatialFieldPrimitive( // _sfp_index, fieldPoints, fieldValues ); // 2011 sfm.UpdateSpatialFieldPrimitive( _sfp_index, fieldPoints, fieldValues, _sfp_index); // 2012 doc.Regenerate(); transaction.Commit(); _lastUpdate = DateTime.Now; } } }
/// <summary> /// Handle Revit Idling event. /// If less time elapsed than the specified interval, return immediately. /// Otherwise, download the current image frile from the specified URL. /// If it has not changed since the last update, return immediately. /// Otherwise, update the spatial field primitive with the new image data. /// Currently, we only display a grey scale image. /// Colour images could be handled as well by defining a custom colour palette. /// </summary> static void OnIdling( object sender, IdlingEventArgs e) { if (DateTime.Now.Subtract(_lastUpdate) > _interval) { Log("OnIdling"); GreyscaleBitmapData data = new GreyscaleBitmapData( _width, _height, _url); byte[] hash = data.HashValue; if (null == _lastHash || 0 != CompareBytes(hash, _lastHash)) { _lastHash = hash; // access active document from sender: Application app = sender as Application; Debug.Assert(null != app, "expected a valid Revit application instance"); UIApplication uiapp = new UIApplication(app); UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uidoc.Document; Log("OnIdling image changed, active document " + doc.Title); Transaction transaction = new Transaction(doc, "Revit Webcam Update"); transaction.Start(); View view = doc.ActiveView; // maybe has to be 3D SpatialFieldManager sfm = SpatialFieldManager.GetSpatialFieldManager( view); if (null == sfm) { sfm = SpatialFieldManager .CreateSpatialFieldManager(view, 1); } if (0 > _sfp_index) { _sfp_index = sfm.AddSpatialFieldPrimitive( _faceReference); } int nPoints = data.Width * data.Height; IList <UV> pts = new List <UV>(nPoints); IList <ValueAtPoint> valuesAtPoints = new List <ValueAtPoint>(nPoints); Face face = _faceReference.GeometryObject as Face; GetFieldPointsAndValues(ref pts, ref valuesAtPoints, ref data, face); FieldDomainPointsByUV fieldPoints = new FieldDomainPointsByUV(pts); FieldValues fieldValues = new FieldValues(valuesAtPoints); sfm.UpdateSpatialFieldPrimitive( _sfp_index, fieldPoints, fieldValues); doc.Regenerate(); transaction.Commit(); _lastUpdate = DateTime.Now; } } }
/// <summary> /// Handle Revit Idling event. /// Return immediately if less time elapsed than /// specified interval. /// Otherwise, download the current image file /// from the specified URL. /// If it has not changed since the last update, /// return immediately. /// Otherwise, update the spatial field primitive /// with the new image data. /// Currently, we only display a grey scale image. /// Colour images could be handled as well by /// defining a custom colour palette. /// </summary> static void OnIdling( object sender, IdlingEventArgs e) { if (DateTime.Now.Subtract(_lastUpdate) > _interval) { Log("OnIdling"); GreyscaleBitmapData data = new GreyscaleBitmapData( _width, _height, _url); byte[] hash = data.HashValue; if (null == _lastHash || 0 != CompareBytes(hash, _lastHash)) { _lastHash = hash; // access active document from sender: // Support both 2011, where sender is an // Application instance, and 2012, where // it is a UIApplication instance: UIApplication uiapp = sender is UIApplication ? sender as UIApplication // 2012 : new UIApplication( sender as Application); // 2011 Debug.Assert(null != uiapp, "expected a valid Revit UIApplication instance"); UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uidoc.Document; Log("OnIdling image changed, active document " + doc.Title); Transaction transaction = new Transaction(doc, "Revit Webcam Update"); transaction.Start(); View view = doc.ActiveView; // maybe has to be 3D SpatialFieldManager sfm = SpatialFieldManager.GetSpatialFieldManager( view); if (null == sfm) { sfm = SpatialFieldManager .CreateSpatialFieldManager(view, 1); } if (0 > _sfp_index) { _sfp_index = sfm.AddSpatialFieldPrimitive( _faceReference); } int nPoints = data.Width * data.Height; IList <UV> pts = new List <UV>(nPoints); IList <ValueAtPoint> valuesAtPoints = new List <ValueAtPoint>(nPoints); // warning CS0618: // GeometryObject is obsolete: Property will be removed. // Use Element.GetGeometryObjectFromReference(Reference) instead //Face face = _faceReference.GeometryObject as Face; // 2011 //Face face = doc.get_Element( _elementId ) // .GetGeometryObjectFromReference( // _faceReference ) as Face; // 2012 // warning CS0618: // Autodesk.Revit.DB.Document.get_Element(Autodesk.Revit.DB.ElementId) is obsolete: // This method has been obsoleted. Use GetElement() instead. //Element eFace = doc.get_Element( _elementId ); // 2012 Element eFace = doc.GetElement(_elementId); // 2013 Face face = eFace.GetGeometryObjectFromReference( _faceReference) as Face; // 2012 GetFieldPointsAndValues(ref pts, ref valuesAtPoints, ref data, face); FieldDomainPointsByUV fieldPoints = new FieldDomainPointsByUV(pts); FieldValues fieldValues = new FieldValues(valuesAtPoints); int result_index; IList <int> registeredResults = sfm.GetRegisteredResults(); if (0 == registeredResults.Count) { AnalysisResultSchema resultSchema = new AnalysisResultSchema( "Schema 1", "Schema 1 Description"); result_index = sfm.RegisterResult( resultSchema); } else { result_index = registeredResults.First(); } // warning CS0618: // UpdateSpatialFieldPrimitive(int, FieldDomainPoints, FieldValues) is obsolete: // This method is obsolete in Revit 2012; use the overload accepting the result index instead. //sfm.UpdateSpatialFieldPrimitive( // _sfp_index, fieldPoints, fieldValues ); // 2011 sfm.UpdateSpatialFieldPrimitive( _sfp_index, fieldPoints, fieldValues, result_index); // 2012 doc.Regenerate(); transaction.Commit(); _lastUpdate = DateTime.Now; } } }