<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Continue another command without closing the current window view in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12664180#M4912</link>
    <description>&lt;P&gt;To be able to modify the active document from a modeless UI, you need to lock the document.&lt;/P&gt;
&lt;P&gt;See &lt;A href="https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-A2CD7540-69C5-4085-BCE8-2A8ACE16BFDD" target="_blank" rel="noopener"&gt;this topic&lt;/A&gt;.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;using (doc.LockDocument())
using (Transaction tr = db.TransactionManager.StartTransaction())
{
    // do your stuff
}&lt;/LI-CODE&gt;</description>
    <pubDate>Mon, 25 Mar 2024 16:24:14 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2024-03-25T16:24:14Z</dc:date>
    <item>
      <title>Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12660883#M4903</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I've just created an interface window in AutoCAD using C# in WPF with the MVVM format. However, I've encountered some issues.&lt;/P&gt;&lt;P&gt;For details:&lt;/P&gt;&lt;P&gt;In the window view, I've created two buttons (SnapOnCommand and SnapOffCommand), which are functioning correctly. However, I would like to implement several other commands in AutoCAD without closing the window application.&lt;/P&gt;&lt;P&gt;Could anyone provide assistance with this matter?&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;// code in view 
&amp;lt;Button Grid.Column="0"
        Grid.Row="6"
        Content="snap on"
        Command="{Binding SnapOnCommand}"
        CommandParameter="{Binding ElementName=MainWindow}" /&amp;gt;
&amp;lt;Button Grid.Column="0"
        Grid.Row="7"
        Content="snap off"
        Command="{Binding SnapOffCommand}"
        CommandParameter="{Binding ElementName=MainWindow}" /&amp;gt;

// code in model

public class HCNModel : BaseViewModel
{

    public void DuplicateAndExplode(out List&amp;lt;ObjectId&amp;gt; explodedObjectIds)
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;

        explodedObjectIds = new List&amp;lt;ObjectId&amp;gt;();

        PromptEntityResult per = ed.GetEntity("\nchoose entity: ");

        if (per.Status != PromptStatus.OK)
            return;

        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
            BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            Entity ent = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;

            if (ent == null)
                return;
            Entity entCopy = ent.Clone() as Entity;

            if (entCopy == null)
                return;
            DBObjectCollection explodedObjects = new DBObjectCollection();
            entCopy.Explode(explodedObjects);
            foreach (DBObject obj in explodedObjects)
            {
                Entity explodedEntity = obj as Entity;

                if (explodedEntity != null)
                {
                    ObjectId explodedObjectId = btr.AppendEntity(explodedEntity);
                    tr.AddNewlyCreatedDBObject(explodedEntity, true);
                    explodedObjectIds.Add(explodedObjectId);
                }
            }

            tr.Commit();
        }
    }


    public void DeleteExplodedObjects(List&amp;lt;ObjectId&amp;gt; explodedObjectIds)
    {
        if (explodedObjectIds == null || explodedObjectIds.Count == 0)
        {
         
            return;
        }

        Document doc = Application.DocumentManager.MdiActiveDocument;
        if (doc == null)
        {
           
            return;
        }

        Database db = doc.Database;

        using (var tr = doc.TransactionManager.StartTransaction())
        {
            foreach (var objectId in explodedObjectIds)
            {
                try
                {
                 
                    Entity ent = tr.GetObject(objectId, OpenMode.ForWrite) as Entity;
                    if (ent != null &amp;amp;&amp;amp; !ent.IsErased) 
                    {
                        
                        if (ent.OwnerId != ObjectId.Null)
                        {
                            
                            ent.Erase();
                        }
                    }
                }
                catch (System.Exception ex)
                {
                   
                    doc.Editor.WriteMessage($"\nerror: {ex.Message}");
                }
            }

            tr.Commit();
        }
    }
}

}

// code in viewmode
public class HCNViewModel : BaseViewModel
{
 private List&amp;lt;ObjectId&amp;gt; _duplicatedObjectIds = new List&amp;lt;ObjectId&amp;gt;();
 private HCNModel _HCNModel;

public HCNModel HCNModel
{
    get { return _HCNModel; }
    set { _HCNModel = value; OnPropertyChanged(nameof(HCNModel)); }
}
 public ICommand SnapOnCommand { get; set; }
 public ICommand SnapOffCommand { get; set; }
public HCNViewModel()
{
    
    HCNModel = new HCNModel();
    SnapOnCommand = new RelayCommand&amp;lt;object&amp;gt;((p) =&amp;gt; true, (p) =&amp;gt;
    {
        List&amp;lt;ObjectId&amp;gt; explodedObjectIds;
        HCNModel.DuplicateAndExplode(out explodedObjectIds);

        _duplicatedObjectIds.AddRange(explodedObjectIds);
    });
    SnapOffCommand = new RelayCommand&amp;lt;object&amp;gt;((p) =&amp;gt; true, (p) =&amp;gt;
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        List&amp;lt;ObjectId&amp;gt; explodedObjectIdsToRemove = new List&amp;lt;ObjectId&amp;gt;();

        using (Transaction tr = doc.TransactionManager.StartTransaction())
        {

            foreach (var objectId in _duplicatedObjectIds)
            {
               
                Entity ent = tr.GetObject(objectId, OpenMode.ForRead) as Entity;
           
                if (ent != null &amp;amp;&amp;amp; ent.OwnerId != ObjectId.Null)
                {

                    explodedObjectIdsToRemove.Add(objectId);
                }
            }

            tr.Commit();
        }
    
        HCNModel.DeleteExplodedObjects(explodedObjectIdsToRemove);

        
        foreach (var objectIdToRemove in explodedObjectIdsToRemove)
        {
            _duplicatedObjectIds.Remove(objectIdToRemove);
        }
    });
}
}&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 23 Mar 2024 16:16:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12660883#M4903</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2024-03-23T16:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12660924#M4904</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Do you show the&amp;nbsp; interface as modal dialog (&lt;A href="https://help.autodesk.com/view/OARX/2023/ENU/?guid=OARX-ManagedRefGuide-__OVERLOADED_ShowModalWindow_Autodesk_AutoCAD_ApplicationServices_Core_Application" target="_blank" rel="noopener"&gt;Application.ShowModalWindow&lt;/A&gt;) or a a modeless dialog (&lt;A href="https://help.autodesk.com/view/OARX/2023/ENU/?guid=OARX-ManagedRefGuide-__OVERLOADED_ShowModelessWindow_Autodesk_AutoCAD_ApplicationServices_Core_Application" target="_blank" rel="noopener"&gt;Application.ShowModelessWindow&lt;/A&gt; or using a &lt;A href="https://help.autodesk.com/view/OARX/2023/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_Windows_PaletteSet" target="_blank" rel="noopener"&gt;PaletteSet&lt;/A&gt;)?&lt;/P&gt;</description>
      <pubDate>Sat, 23 Mar 2024 16:52:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12660924#M4904</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-03-23T16:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12661529#M4905</link>
      <description>&lt;BR /&gt;No, I've created a button on the ribbon tab, and when I click on it, the window view appears. Then, I click a button in the window view. I want to be able to execute other commands while the window view is still open, rather than having to close it.</description>
      <pubDate>Sun, 24 Mar 2024 03:13:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12661529#M4905</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2024-03-24T03:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12661640#M4906</link>
      <description>&lt;P&gt;You didn't answer&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;'s question.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need to show how you are showing the Window when you click the button on the Palette.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To do anything while a window is visible you must show the window &lt;EM&gt;&lt;STRONG&gt;modelessly&lt;/STRONG&gt;&lt;/EM&gt; (using Application.ShowModelessWindow()). If you show the window by calling ShowModalWindow(), it blocks and will not allow you to do anything until the Window is closed.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2024 06:25:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12661640#M4906</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-03-24T06:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12662110#M4907</link>
      <description>&lt;P&gt;&lt;SPAN&gt;This is the code to display the menu. However, I'm unsure how to modify it to enable other commands without closing the window. Could you please assist me in fixing it?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;public class Ribbon : IExtensionApplication
{
    public void Initialize()
    {
        try
        {
            
            if (ComponentManager.Ribbon != null)
            {
               
                CreateMenu();
            }
            else
            {
               
                Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSetCreated += new EventHandler(RibbonServices_RibbonPaletteSetCreated);
            }
        }
        catch { }

    }

    private void RibbonServices_RibbonPaletteSetCreated(object sender, EventArgs e)
    {
        RibbonPaletteSet_Loaded
        Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.Load += new Autodesk.AutoCAD.Windows.PalettePersistEventHandler(RibbonPaletteSet_Loaded);
    }

    private void RibbonPaletteSet_Loaded(object sender, PalettePersistEventArgs e)
    {
       
        CreateMenu();
    }
//////&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2024 15:32:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12662110#M4907</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2024-03-24T15:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12662124#M4908</link>
      <description>&lt;P&gt;The code you posted is not the code that shows the window when the user clicks the button on the pallet set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That is what you need to show.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2024 15:48:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12662124#M4908</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-03-24T15:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12662188#M4909</link>
      <description>&lt;P&gt;maybe this code. I have created to input on ribbon button&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="traiduong014969_0-1711298348052.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1341106i2E48DC3BCDD2C8D7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="traiduong014969_0-1711298348052.png" alt="traiduong014969_0-1711298348052.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;or&amp;nbsp;&lt;/P&gt;&lt;P&gt;public class SendCommand : ICommand&lt;BR /&gt;{&lt;BR /&gt;event EventHandler ICommand.CanExecuteChanged&lt;BR /&gt;{&lt;BR /&gt;add { }&lt;BR /&gt;remove { }&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public bool CanExecute(object parameter) { return true; }&lt;/P&gt;&lt;P&gt;public void Execute(object parameter)&lt;BR /&gt;{&lt;BR /&gt;if (parameter is RibbonCommandItem btn)&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;Document doc = AcadApp.DocumentManager.MdiActiveDocument;&lt;BR /&gt;doc.SendStringToExecute((string)btn.CommandParameter + " ", true, false, true);&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2024 17:03:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12662188#M4909</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2024-03-24T17:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12662245#M4910</link>
      <description>&lt;P&gt;ShowDialog() displays a modal window that must be closed before you can do anything. If you want the window to remain open while you execute commands or something else, you have to use the &lt;A href="https://help.autodesk.com/view/OARX/2022/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_ApplicationServices_Core_Application_ShowModelessWindow_System_Windows_Window" target="_blank" rel="noopener"&gt;ShowModelessWindow&lt;/A&gt;() method of the Application object.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2024 17:20:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12662245#M4910</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-03-24T17:20:58Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12664084#M4911</link>
      <description>&lt;P&gt;&lt;SPAN&gt;es, I tested this code, and it works. However, when I click the button with the ICommand "Snapon," lines of code appear with errors.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="traiduong014969_0-1711381224860.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1341449i974503F8A38BB734/image-size/medium?v=v2&amp;amp;px=400" role="button" title="traiduong014969_0-1711381224860.png" alt="traiduong014969_0-1711381224860.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="traiduong014969_1-1711381495260.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1341454iBECD58BE974BD063/image-size/medium?v=v2&amp;amp;px=400" role="button" title="traiduong014969_1-1711381495260.png" alt="traiduong014969_1-1711381495260.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 15:46:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12664084#M4911</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2024-03-25T15:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12664180#M4912</link>
      <description>&lt;P&gt;To be able to modify the active document from a modeless UI, you need to lock the document.&lt;/P&gt;
&lt;P&gt;See &lt;A href="https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-A2CD7540-69C5-4085-BCE8-2A8ACE16BFDD" target="_blank" rel="noopener"&gt;this topic&lt;/A&gt;.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;using (doc.LockDocument())
using (Transaction tr = db.TransactionManager.StartTransaction())
{
    // do your stuff
}&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 25 Mar 2024 16:24:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12664180#M4912</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-03-25T16:24:14Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12664399#M4913</link>
      <description>&lt;P&gt;Download &lt;A href="https://github.com/ActivistInvestor/CommunityToolKitExtensions/blob/main/DocumentRelayCommand.cs" target="_blank" rel="noopener"&gt;this file&lt;/A&gt; and add it to your project.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then, make the following changes to your code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;/// change this line:
SnapOnCommand = new RelayCommand&amp;lt;object&amp;gt;((p) =&amp;gt; true, (p) =&amp;gt; .....

/// To this:
SnapOnCommand = new DocumentRelayCommand&amp;lt;object&amp;gt;((p) =&amp;gt; true, (p) =&amp;gt; ....&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;/// Change this line:
SnapOffCommand = new RelayCommand&amp;lt;object&amp;gt;((p) =&amp;gt; true, (p) =&amp;gt;

/// To this:
SnapOffCommand = new DocumentRelayCommand&amp;lt;object&amp;gt;((p) =&amp;gt; true, (p) =&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you make these changes, then you do not have to manually lock the document, and your code will behave like a command WRT document locking and UNDO/REDO, and your command will correctly not be available when there is no active document.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 20:20:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12664399#M4913</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-03-25T20:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: Continue another command without closing the current window view</title>
      <link>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12666669#M4914</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4476837"&gt;@ActivistInvestor&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;. Thank you for your help&lt;/P&gt;&lt;P&gt;Finally this code work for me&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 14:48:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/continue-another-command-without-closing-the-current-window-view/m-p/12666669#M4914</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2024-03-26T14:48:19Z</dc:date>
    </item>
  </channel>
</rss>

