.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MPSPLIT in C#

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
tristan.jonas8XAAW
136 Views, 3 Replies

MPSPLIT in C#

Hello all,

 

I'm trying to accomplish the same effect as MPSPLIT programmatically, which is to say, use a polyline to split an MPolygon in half. I haven't been able to find ANYTHING online about splitting MPolygons in C#, there doesn't appear to be a known way to achieve this the "right" way so I tried to use this:

                        // Run MSPLIT command on MPolygons with mixed points
                        foreach (MPolygon mPolygon in mixedPolygons)
                        {
                            try
                            {
                                string msplitCommand = $"_.MSPLIT\n{mPolygon.ObjectId}\nSelect\n{outlinePolyline.ObjectId}\nYes\n";
                                doc.SendStringToExecute(msplitCommand, true, false, false);
                            }
                            catch (System.Exception ex)
                            {
                                ed.WriteMessage($"\nError splitting MPolygon with handle {mPolygon.Handle}: {ex.Message}");
                            }
                        }


However, neither the .Handle nor the .ObjectId works as something that can be entered into the Command Line to select the objects necessary to complete the command. I don't know how to invoke MPSPLIT functionality programmatically and I don't know how to inject object selections into SendStringToExecute. Any ideas?

tristanjonas8XAAW_0-1721928403090.png


Thank you,

Tristan

3 REPLIES 3
Message 2 of 4

Don't use SendStringToExecute().  This API is the source of more headaches, confusion, and wasted time than any other API in the product.

 

It doesn't execute the command until after the code that calls it has exited and returned control to AutoCAD.

 

You can use the Editor's Command() method to drive pretty-much any command that you can interact with via the command line. It's fully-synchronous, so as soon as it returns the operation has completed. Unlike SendStringToExecute(), the Command() method accepts Points, strings, numbers, ObjectIds, and SelectionSets. 

Message 3 of 4

Hi ActivistInvestor,

You made quick work of that, somehow I'd never heard of the ed.Command method as an alternative to the SendStringToExecute, obviously that's much better. I try to avoid using commands as a regular part of my code but for now this suits my purposes just fine. Here is the final implementation:

 

 

        private static void SplitMPolygons(IEnumerable<MPolygon> mixedPolygons, Polyline outlinePolyline, Editor ed)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            foreach (MPolygon mPolygon in mixedPolygons)
            {
                // Create a SelectionSet containing the MPolygon and the Polyline
                SelectionSet mPolygonSet = SelectionSet.FromObjectIds(new ObjectId[] { mPolygon.ObjectId });
                SelectionSet polylineSet = SelectionSet.FromObjectIds(new ObjectId[] { outlinePolyline.ObjectId });

                // Add the command to the string
                ed.Command("_.MPSPLIT",
                    mPolygonSet,
                    "_Select",
                    polylineSet,
                    "",
                    "_Yes",
                    "_Select",
                    "");

            }
        }

 

 

 
Some notes for anyone else who may have this problem:

1. The items passed into ed.Command have to be converted to SelectionSet variables first
2. After passing in polylineSet, you have to confirm your selection, which you'd ordinarily do just by typing "Enter". Tried using "/n" and it didn't work, but an empty string "" does.
3. There's an unfortunate side case that occurs if an edge of the MPolygon (or a singular point on the mpolygon, or more than one intersection segment) lies along the Polyline you're using to cut the line, which triggers this message:

"Selected mpolygon cannot be split in that way, please try again."

I am kind of stumped on how to deal with that, might just have to be part of the user responsibility to handle them manually but I'd prefer a more reasonable solution if anybody has one.

But either way, if it happens there doesn't appear to be an obvious way to cancel the middle of the command (sending \x03\x03 doesn't work to exit the command), but sending "_Select" and then "" again finishes out the command if it's invalid, and it doesn't cause any issues if it does. I really hate it, but it does work, and if you have any suggestions please advise me, otherwise this will do.

Here's a diagram of the three scenarios that cause failures in MPSPLIT:

tristanjonas8XAAW_0-1721953297189.png

 

Message 4 of 4

Unfortunately, the Command() method cannot provide partial input to a command. It's an all-or-nothing affair. If not all the input needed to complete the command is included, it will cancel the command as if you pressed ^C/ESC on the keyboard.

 

You could try doing up-front analysis using Entity.IntersectWith() to find intersections and singularities, but other than that, there's no way to deal with it once the Command() method has been called.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report