Using a modal .NET dialog to display AutoCAD object properties

 

Firstly, a big thanks for all your comments on the first anniversary post. It's good to know that people are finding this blog useful, and I hope the flow of ideas (internal and external) doesn't dry up anytime soon. So keep the comments coming! :-)

This post is going to start a sequence of posts that look at how to integrate forms into AutoCAD. This post looks at modal forms, and later on we'll look more at modeless forms and - in particular - palettes.

Just to be clear, these posts will focus on the basic integration - the more advanced activity of detailed property display (etc.) are left as an exercise for the reader. For example, in today's code we're simply going to get the type of an object and put that text into our dialog. Nothing very complex, but it shows the basic interaction between AutoCAD objects and WinForms.

Before we get started, I should very quickly define "modal" and "modeless", for those that aren't familiar with the terminology. Modal dialogs take exclusive control of an application's user-input functions, while modeless dialogs can co-exist with other modeless dialogs and user-input handling. Which means that the two types of dialog have different issues to deal with (in terms of how the assumptions they make on accessing data etc.).

So next we need to create our form... I'm not going to step through the process here - we'll focus on the code - so to make life easier I've packaged up the source here.

Here's the C# code for the command class. It's very simple: it checks the pickfirst selection and uses the first object as input for the form (assigning the object ID to the form, which will then go away and retrieve the object's type). At this stage we're just supporting one object - we're not going through the effort of determining shared properties across objects etc. We then use Application.ShowModalDialog() to show the form inside AutoCAD.

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using System;

using CustomDialogs;


namespace CustomDialogs

{

  public class Commands

  {

    [CommandMethod("vt",CommandFlags.UsePickSet)]

    public void ViewType()

    {

      Editor ed =

        Application.DocumentManager.MdiActiveDocument.Editor;


      TypeViewerForm tvf = new TypeViewerForm();

      PromptSelectionResult psr =

        ed.GetSelection();

      if (psr.Value.Count > 0)

      {

        ObjectId selId = psr.Value[0].ObjectId;

        tvf.SetObjectId(selId);

      }

      if (psr.Value.Count > 1)

      {

        ed.WriteMessage(

          "/nMore than one object was selected: only using the first./n"

        );

      }

      Application.ShowModalDialog(null, tvf, false);

    }

  }

}

The form itself is a little more complex (but barely). It contains a function that can be used to set the active object (by its ID, as mentioned above) which goes away and opens the object, getting its type. The form also contains a "browse" button, which can be used to change the actively selected object.

Here's the C# code for the form:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;


namespace CustomDialogs

{

  public partial class TypeViewerForm : Form

  {

    public TypeViewerForm()

    {

      InitializeComponent();

    }


    public void SetObjectText(string text)

    {

      typeTextBox.Text = text;

    }


    public void SetObjectId(ObjectId id)

    {

      if (id == ObjectId.Null)

      {

        SetObjectText("");

      }

      else

      {

        Document doc =

          Autodesk.AutoCAD.ApplicationServices.

            Application.DocumentManager.MdiActiveDocument;

        Transaction tr =

          doc.TransactionManager.StartTransaction();

        using (tr)

        {

          DBObject obj = tr.GetObject(id, OpenMode.ForRead);

          SetObjectText(obj.GetType().ToString());

          tr.Commit();

        }

      }

    }

    private void closeButton_Click(object sender, EventArgs e)

    {

      this.Close();

    }


    private void browseButton_Click(object sender, EventArgs e)

    {

      DocumentCollection dm =

        Autodesk.AutoCAD.ApplicationServices.

          Application.DocumentManager;

      Editor ed =

        dm.MdiActiveDocument.Editor;


      Hide();

      PromptEntityResult per =

        ed.GetEntity("/nSelect entity: ");

      if (per.Status == PromptStatus.OK)

      {

        SetObjectId(per.ObjectId);

      }

      else

      {

        SetObjectId(ObjectId.Null);

      }

      Show();

    }

  }

}

Here's what happens when you run the VT command and select a circle:

Modal_dialog_1

As an aside... I'm going on holiday on Wednesday for a week. We have our annual football (meaning soccer, of course) tournament being held this year in the UK. Teams from Autodesk offices from around the world (it used to have a European focus, but it's gained in popularity in the last few years) will fly in and take part. I'll be playing for one of the Swiss teams, and it'll certainly be lots of fun to catch up with old friends from other locations. The tournament is at the weekend, but I'm taking some days off either side to catch up with family and friends. I doubt I'll have time to queue up additional posts for while I'm away, so this is just to let you know that things may go a little quiet for the next week or so.

June 25, 2007 in AutoCAD, AutoCAD .NET | Permalink

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/876965/19579964

 
<think> 嗯,用户这次的问题很明确,是要了解Bootstrap中modal-dialogmodal-dialog-scrollable的使用方法和特性。从用户提供的站内引用来看,用户似乎正在开发一个包含模态框的web应用,其中引用了人工智能选择功能的模态框代码片段。不过用户特别说明这些引用仅供参考,真正的需求是关于这两个类名的技术细节。 用户可能是前端开发者,正在使用Bootstrap框架构建交互界面。考虑到用户特意区分了modal-dialogmodal-dialog-scrollable,应该是遇到了滚动条控制的具体需求。这类问题通常出现在需要处理长内容模态框的场景中,比如用户可能正在设计包含大量表单字段或长文本的弹窗。 关于modal-dialog的基础特性:这是创建模态框容器的核心类,控制着模态框的基本尺寸和位置。默认居中显示,宽度自适应。用户可能需要注意配合modal-content使用,这是Bootstrap的标准结构。 而modal-dialog-scrollable这个类就更有意思了。它的设计解决了模态框内容过长时的显示问题——不是让整个页面滚动,而是让模态框内部滚动。这个特性在移动端尤其重要,能防止模态框内容溢出视口。用户可能正在处理类似长表格或日志展示的需求。 需要重点说明两者的组合用法:当内容高度不确定时,同时使用modal-dialogmodal-dialog-scrollable是最佳实践。记得提醒用户滚动条只作用于modal-body区域,这样页头页脚能保持固定。 用户可能还会遇到的一个陷阱是响应式设计:在超小屏幕上,Bootstrap会自动将模态框宽度设为全屏,这时候滚动条行为可能会变化。这个细节值得在回答中强调。 考虑到这是比较具体的组件使用问题,后续可以建议用户查阅Bootstrap官方文档的Modal版块,特别是Scrollable部分。不过当前回答应该已经覆盖了核心用法。 </think> 好的,关于 Bootstrap 中的 `modal-dialog` 和 `modal-dialog-scrollable` 类,以下是它们的使用方法、特性及区别: **核心概念:** 在 Bootstrap 的模态框 (Modal) 结构中,`modal-dialog` 是 **必需的** 核心容器类,它包裹着 `modal-content`(包含模态框的头部、主体和底部)。`modal-dialog-scrollable` 是一个 **可选的修饰符类**,用于控制模态框内容的滚动行为。 ### 1. `modal-dialog` * **功能与作用:** * 定义模态框对话框本身的容器。 * 控制模态框的基本布局、最大宽度、位置(居中)。 * 设置模态框相对于视口的默认外边距。 * 是构建任何 Bootstrap 模态框的基础结构元素。 * **特性:** * **居中显示:** 默认将模态框垂直水平居中于视口中。 * **响应式宽度:** 有预设的最大宽度 (`max-width`)。在小屏幕上(如手机),模态框会占据接近全屏宽度;在中等和大屏幕上,模态框会有固定的最大宽度(默认为 `500px`,可以通过 `.modal-lg`, `.modal-xl`, `.modal-fullscreen` 或自定义 CSS 修改)。 * **外边距:** 在所有视口尺寸上都有适当的外边距 (`margin`),确保模态框不与视口边缘接触。 * **定位:** 使用 `position: relative`。 * **基本结构 (必需的):** ```html <div class="modal fade" id="myModal" tabindex="-1" aria-hidden="true"> <div class="modal-dialog"> <!-- 核心容器 --> <div class="modal-content"> <!-- 内容容器 --> <div class="modal-header">...</div> <div class="modal-body">...</div> <div class="modal-footer">...</div> </div> </div> </div> ``` * **使用方法:** * 任何 Bootstrap 模态框都 **必须** 在 `.modal` 元素内部包含一个 `.modal-dialog` 元素。 * 它是模态框视觉呈现的骨架。 ### 2. `modal-dialog-scrollable` * **功能与作用:** * 当模态框的内容(主要是 `.modal-body`)高度 **过长**,超出视口或模态框自身最大高度时,启用模态框 **内部滚动**。 * 确保 **只有 `.modal-body` 区域会滚动**,而 `.modal-header` 和 `.modal-footer` 保持固定在原位。 * **特性:** * **内部滚动:** 将 `overflow-y: auto` 应用于 `.modal-body`。 * **固定页眉页脚:** 通过限制 `.modal-content` 的最大高度 (`max-height`) 并启用 `display: flex` 和 `flex-direction: column`,使得 `.modal-header` 和 `.modal-footer` 不会随内容滚动而消失,始终可见。 * **优化长内容:** 专门设计用于处理模态框中包含大量文本、长列表或表单等导致高度过大的情况。 * **使用方法:** * 将 `modal-dialog-scrollable` 类 **添加** 到已有的 `.modal-dialog` 类旁边。 ```html <div class="modal fade" id="myLongModal" tabindex="-1" aria-hidden="true"> <div class="modal-dialog modal-dialog-scrollable"> <!-- 添加 scrollable 类 --> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">长内容标题</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <!-- 这里是非常非常长的内容(文本、列表、表格等) --> <p>...</p> <p>...</p> <!-- 更多内容,足以产生垂直滚动条 --> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button> <button type="button" class="btn btn-primary">保存</button> </div> </div> </div> </div> ``` * **关键效果:** * 当 `.modal-body` 内容过长时,**仅该区域** 会出现垂直滚动条。 * 用户滚动查看长内容时,上方的标题 (`modal-header`) 和下方的按钮 (`modal-footer`) **保持固定不动**,始终可见,提供良好的用户体验。 ### 总结对比 | 特性 | `modal-dialog` | `modal-dialog-scrollable` | | :----------------------- | :------------------------------------------- | :----------------------------------------------------------- | | **必要性** | **必需** - 模态框的核心结构组件。 | **可选** - 仅在模态框内容可能很长时才需要。 | | **主要功能** | 定义模态框容器,控制基本布局、宽度和居中。 | 启用模态框内部滚动 (仅 `.modal-body` 滚动),固定页眉页脚。 | | **滚动行为** | 如果内容过高,可能导致 **整个视窗(页面)滚动** (不理想)。 | 强制 **模态框内部 `.modal-body` 区域滚动**,页面本身不滚动。 | | **页眉页脚 (`header`/`footer`)** | 如果内容过长滚动,页眉页脚会 **移出可视区域**。 | 页眉页脚 **固定** 在模态框顶部和底部,滚动时始终可见。 | | **使用场景** | 所有模态框的基础。 | 包含大量文本、长列表、大型表单、详细数据展示等 **长内容** 的模态框。 | **最佳实践建议:** * 总是使用 `.modal-dialog` 作为模态框内容的外层容器。 * 当您预期模态框的 `.modal-body` 内容可能会很长,超出屏幕高度时,**务必添加 `.modal-dialog-scrollable` 类** 到 `.modal-dialog` 上。这能提供更好的用户体验,防止整个页面滚动或关键操作按钮(在 Footer 中)被推出视口。 * 使用 `.modal-lg`, `.modal-xl`, `.modal-fullscreen` 等类可以与 `.modal-dialog-scrollable` 一起使用,以控制对话框的宽度/全屏状态,同时保持内部滚动特性。 #### 引用说明 Bootstrap 的模态框组件设计确保了弹窗内容的灵活展示,其中 `modal-dialog` 控制基础结构,而 `modal-dialog-scrollable` 则专门处理长内容场景,两者共同优化了用户交互体验[^1][^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值