Since AutoCAD .NET API came along, we can quite easily to show custom data as entity tool tip with only a few lines of code. Below is sample code of doing a simple entity tool tip.
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using CadApp = Autodesk.AutoCAD.ApplicationServices.Application; [assembly: CommandClass(typeof(CustomToolTip.SimpleToolTip))] namespace CustomToolTip { public class SimpleToolTip { private bool _enabled = false; private Document _dwg = null; public SimpleToolTip() { _dwg = CadApp.DocumentManager.MdiActiveDocument; } [CommandMethod("SimpleTip")] public void RunSimpleToolTip() { var dwg = CadApp.DocumentManager.MdiActiveDocument; var ed = dwg.Editor; if (!_enabled) { SetPointMonitorEventHandler(true); _enabled = true; ed.WriteMessage( string.Format("\nSIMPLE TIP is enabled in drawing {0}.\n", System.IO.Path.GetFileName(dwg.Name))); } else { SetPointMonitorEventHandler(false); _enabled = false; ed.WriteMessage( string.Format("\nSIMPLE TIP is disabled in drawing {0}.\n", System.IO.Path.GetFileName(dwg.Name))); } } #region private methods private void SetPointMonitorEventHandler(bool attach) { if (attach) { _dwg.Editor.PointMonitor += Editor_PointMonitor; } else { _dwg.Editor.PointMonitor -= Editor_PointMonitor; } } private void Editor_PointMonitor(object sender, PointMonitorEventArgs e) { var fullPaths = e.Context.GetPickedEntities(); if (fullPaths.Length == 0) return; ObjectId entId = ObjectId.Null; foreach (var path in fullPaths) { if (!path.IsNull) { var ids = path.GetObjectIds(); if (ids.Length>0) { entId = ids[0]; } } if (!entId.IsNull) break; } if (!entId.IsNull) { // Now, with entity's ID in handle, one can decide if the entity // is the target entity to show custom tool tip, and yes, what // information would be shown. Here I simply show DxfName e.AppendToolTipText( "DXF NAME: " + entId.ObjectClass.DxfName); } } #endregion } }
This video clip shows how the code works. One would notice that even through the method of the PointMonitorEventArgs used to set custom tool tip is called AppendToolTipText, it does not append the custom tool tip text to existing AutoCAD entity tool tip (when AutoCAD's "RollOverTips" system variable is set to 1). Instead, the custom tool tip text replaces AutoCAD built-in tool tip. Also one may notice the command method is an "instance" method, as opposed to "static" method. That means, when the command method is called (user enters command "SimpleTip"), the first time, AutoCAD creates an instance of the class SimpleToolTip on each document where the command runs. Because each drawing has its own SimpleToolTip class, I can safely have a member field in the class _dwg to hold reference to the drawing document, and use it to attach/detach Editor.PointMonitor event handler.
Although using PointMonitorEventArg.AppendToolTipText() makes it very easy to show custom tool tip, it does not provide further data formatting options to make custom tool tip better. There were a couple of discussions on AutoCAD entity tool tip in the Autodesk's discussion forum, here and here, where one of the Autodesk Expert Elite, Keith Brown, posted some code that provided a quite elegant solution. As Keith's code shows, the entry point for the custom code to manipulate AutoCAD's entity tool tip is to handle Autodesk.Windows.ComponentManager.ToolTipOpened event, where the custom tool tip content is "injected" into the showing tool tip window (WPF Visual object).
By learning from Keith's code, I came up with yet another way to show custom entity tool tip in AutoCAD: using code directly creates an instance of ToolTip class (Autodesk.Internal.Windows.ToolTip) and shows it as needed.
Firstly, this is the class that shows/closes custom tool tips MyEntityToolTip:
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using CadApp = Autodesk.AutoCAD.ApplicationServices.Application; [assembly: CommandClass(typeof(CustomToolTip.MyEntityToolTip))] namespace CustomToolTip { public class MyEntityToolTip { private Document _dwg = null; private Autodesk.Internal.Windows.ToolTip _tipWindow = null; private bool _rolloverEnabled = false; private ObjectId _currentEntId = ObjectId.Null; private short _builtInRollTip = 1; public MyEntityToolTip() { _dwg = CadApp.DocumentManager.MdiActiveDocument; } [CommandMethod("EntInfo")] public void ShowEntityInfo() { while(true) { var opt = new PromptEntityOptions( "\nSelect an entity"); opt.Keywords.Add("eXit"); opt.Keywords.Default = "eXit"; var res = _dwg.Editor.GetEntity(opt); if (res.Status==PromptStatus.OK) { ShowEntityInfo(res.ObjectId); _dwg.Editor.GetString("\nPress any key to continue..."); if (_tipWindow!=null) { _tipWindow.Close(); _tipWindow = null; } } else { break; } } } [CommandMethod("MyRollOverTip")] public void SetRolloverTip() { if (!_rolloverEnabled) { SetRolloverEventHandler(true); _rolloverEnabled = true; _dwg.Editor.WriteMessage( "\nMY ROLLOVER TOOLTIP is enabled\n"); } else { SetRolloverEventHandler(false); _rolloverEnabled = false; _dwg.Editor.WriteMessage( "\nMY ROLLOVER TOOLTIP is disabled\n"); } } #region private methods private void ShowEntityInfo(ObjectId entId) { if (_tipWindow!=null) { _tipWindow.Close(); _tipWindow = null; } _tipWindow = new Autodesk.Internal.Windows.ToolTip(); _tipWindow.MaxWidth = 200; _tipWindow.MinWidth = 100; var myContent = new ToolTipCustomContent(); myContent.HeaderText= string.Format("Entity ID: {0}", entId.ToString()); // Deliberately pass in long test string myContent.DetailText = "This entity's information should be obtained " + "based on the drafting operation requirements."; myContent.FooterText= string.Format("Entity Type: {0}", entId.ObjectClass.DxfName); System.Drawing.Bitmap picture; if (entId.ObjectClass.DxfName.ToUpper() == "CIRCLE") picture = Properties.Resources.Koala; else picture = Properties.Resources.Penguins; myContent.DetailPicture = picture; _tipWindow.Content = myContent; _tipWindow.Background = System.Windows.Media.Brushes.LightCyan; _tipWindow.Show(); } private void SetRolloverEventHandler(bool enable) { if (enable) { _builtInRollTip = (short)CadApp.GetSystemVariable("ROLLOVERTIPS"); CadApp.SetSystemVariable("ROLLOVERTIPS", 0); _dwg.Editor.Rollover += Editor_Rollover; } else { _dwg.Editor.Rollover -= Editor_Rollover; CadApp.SetSystemVariable("ROLLOVERTIPS", _builtInRollTip); } } private void Editor_Rollover(object sender, RolloverEventArgs e) { ObjectId entId = ObjectId.Null; var fullPath = e.Highlighted; if (!fullPath.IsNull) { var ids = fullPath.GetObjectIds(); if (ids.Length>0) { entId = ids[0]; } } if (entId.IsNull) { _currentEntId = ObjectId.Null; if (_tipWindow != null) { _tipWindow.Close(); } } else { if (entId!=_currentEntId) { ShowEntityInfo(entId); _currentEntId = entId; } } } #endregion } }
From the code one can see that I create/show/close an Autodesk.Internal.Windows.ToolTip instance in Editor.Rollover event handler. Because AutoCAD would still show its built-in rollover tool tip if system variable "ROLLOVERTIPS" is set 1, regardless my custom tool tip shows or not, I set the system variable "ROLLOVERTIPS" to 0 when my custom tool tip is enabled, and restore it back when my custom tool tip is disabled.
The actual content displayed by Autodesk.Internal.Widnows.ToolTip is a WPF UserControl. Here is its XAML code:
<UserControl x:Class="CustomToolTip.ToolTipCustomContent" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:CustomToolTip" mc:Ignorable="d" d:DesignHeight="150" d:DesignWidth="150"> <Grid x:Name="MyContent"> <Grid.RowDefinitions> <RowDefinition Height="26" /> <RowDefinition Height="25*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <StackPanel x:Name="stpContentHeader" Grid.Row="0" Background="#FFFDFD44"> <TextBlock x:Name="txtHeader" Text="" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,5,5,5"/> </StackPanel> <StackPanel x:Name="stpContentDetail" Grid.Row="1" Orientation="Vertical"> <TextBlock x:Name="txtDetail" Text="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,0,5,0" TextWrapping="Wrap"/> <Image x:Name="imgDetail" Visibility="Collapsed" Margin="5,5,5,5" Stretch="Fill" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> </Image> </StackPanel> <StackPanel x:Name="stpContentFooter" Grid.Row="2" Background="#FFFDFD44"> <TextBlock x:Name="txtFooter" Text="" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,5,5,5"/> </StackPanel> </Grid> </UserControl>
Here is WPF UserControl's code-behind:
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace CustomToolTip { /// <summary> /// Interaction logic for ToolTipCustomContent.xaml /// </summary> public partial class ToolTipCustomContent : UserControl { public ToolTipCustomContent() { InitializeComponent(); } public StackPanel ContentHeaderPanel { get { return stpContentHeader; } } public StackPanel ContentDetailPanel { get { return stpContentDetail; } } public StackPanel ContentFooterPanel { get { return stpContentFooter; } } public string HeaderText { set { txtHeader.Text = value; } get { return txtHeader.Text; } } public string DetailText { set { txtDetail.Text = value; } get { return txtDetail.Text; } } public string FooterText { set { txtFooter.Text = value; } get { return txtFooter.Text; } } public System.Drawing.Bitmap DetailPicture { set { ImageSource source = null; if (value != null) { source = value.ToWpfImageSource(); } if (source==null) { imgDetail.Visibility = Visibility.Collapsed; } else { imgDetail.Source = source; imgDetail.Visibility = Visibility.Visible; } } } } }
In order to display custom tool tip easier, I make the tool tip to be displayed in 3 portions: header (text), detail (text and picture) and footer (text). And I expose the controls (TextBlock and Image) directly as the UserControl's properties, so that they can be set easily. Obviously, the UserControl can be fill up dynamically with any suitable control depending on data to be displayed. I added 2 PNG pictures into the project's resources, and display one picture when mouse cursor hover on CIRCLE, otherwise display another picture. To show picture in WPF's Image control, I need to convert System.Drawing.Bitmap object into System.Windows.Media.ImageSource object:
using System.Drawing.Imaging; using System.Windows.Media.Imaging; namespace CustomToolTip { public static class ImageSourceExtension { public static System.Windows.Media.ImageSource ToWpfImageSource( this System.Drawing.Bitmap picture) { using (var memory = new System.IO.MemoryStream()) { picture.Save(memory, ImageFormat.Png); memory.Position = 0; var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); return bitmapImage; } } } }
Now, go to this video clip to see the custom tool tip in action.
For the sake of completeness, I also tried Mr. Keith Brown's approach of handling ComponentManager.ToolTipOpened event. However, I used Editor.Rollover event handler instead of Editor.PointMonitor event handler to identify entity, for which the custom tool tip shows. Of course I use the same WPF UserControl as the content of the custom tool tip. See code below:
using System; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using CadApp = Autodesk.AutoCAD.ApplicationServices.Application; using Autodesk.Windows; [assembly: CommandClass(typeof(CustomToolTip.CustomRolloverToolTip))] namespace CustomToolTip { public class CustomRolloverToolTip { private Document _dwg = null; private bool _rolloverEnabled = false; private ObjectId _targetEntId = ObjectId.Null; public CustomRolloverToolTip() { _dwg = CadApp.DocumentManager.MdiActiveDocument; ComponentManager.ToolTipOpened += ComponentManager_ToolTipOpened; } [CommandMethod("CustomToolTip")] public void ShowCustomToolTip() { if (!_rolloverEnabled) { SetRolloverEventHandler(true); _rolloverEnabled = true; _dwg.Editor.WriteMessage( "\nCUSTOM ROLLOVER TOOLTIP is enabled\n"); } else { SetRolloverEventHandler(false); _rolloverEnabled = false; _dwg.Editor.WriteMessage( "\nCUSTOM ROLLOVER TOOLTIP is disabled\n"); } } #region private methods private void ComponentManager_ToolTipOpened(object sender, EventArgs e) { if (_targetEntId.IsNull) return; if (Convert.ToInt32(CadApp.GetSystemVariable("ROLLOVERTIPS")) == 0) return; var tipWindow = sender as Autodesk.Internal.Windows.ToolTip; if (tipWindow == null) return; tipWindow.MaxWidth = 200; tipWindow.MinWidth = 100; ShowEntityInfo(tipWindow); } private void SetRolloverEventHandler(bool enable) { if (enable) { _dwg.Editor.Rollover += Editor_Rollover; } else { _dwg.Editor.Rollover -= Editor_Rollover; } } private void Editor_Rollover(object sender, RolloverEventArgs e) { ObjectId entId = ObjectId.Null; var fullPath = e.Highlighted; if (!fullPath.IsNull) { var ids = fullPath.GetObjectIds(); if (ids.Length > 0) { entId = ids[0]; } } _targetEntId = entId; } private void ShowEntityInfo(Autodesk.Internal.Windows.ToolTip tipWindow) { var myContent = new ToolTipCustomContent(); myContent.HeaderText = string.Format("Entity ID: {0}", _targetEntId.ToString()); // Deliberately pass in long test string myContent.DetailText = "This entity's information should be obtained " + "based on the drafting operation requirements."; myContent.FooterText = string.Format("Entity Type: {0}", _targetEntId.ObjectClass.DxfName); System.Drawing.Bitmap picture; if (_targetEntId.ObjectClass.DxfName.ToUpper() == "CIRCLE") picture = Properties.Resources.Koala; else picture = Properties.Resources.Penguins; myContent.DetailPicture = picture; tipWindow.Content = myContent; tipWindow.Background = System.Windows.Media.Brushes.LightCyan; _targetEntId = ObjectId.Null; } #endregion } }
See this video clip showing the code in action.
If watching the 2 video clips carefully, one would notice the difference of visual effect between the 2 approaches:
With handling ComponentManager.ToolTipOpened event, the custom code has not control of when tool tip is shown and when is closed. The custom code does is to "inject" custom tool tip data and display format via a WPF ContentControl, usually a UserControl. Meanwhile, the custom tool tip is controlled by "ROLLOVERTIPS" system variable, as built-in rollover tool tip is. However, if the tool tip carries heavier data, such as picture, as the video shows, the action may be a bit sluggish, because ComponetManager shows new ToolTip window whenever the mouse cursor moves, even the cursor never leaves the entity being hovered.
With showing own ToolTip window in Editor.Rollover event handler, my code only closes the ToolTip window when mouse cursor leaves the entity being hovered, so the tool tip display is smoother.
This video clip shows the visual effect difference of the 2 approaches.
3 comments:
I want to make a software, that will take 2D Elevation as input and convert it to 3D Model. I have sample 2D Elevations. Please share your email ID, i will share elevation type.
tell me your email my email is kfkfzwd@163.com
Hi sir,
please update this post video!
Thanks!
Post a Comment