Selections Sets and Multileader Styles

Selections Sets and Multileader Styles

sonny3g
Collaborator Collaborator
1,794 Views
8 Replies
Message 1 of 9

Selections Sets and Multileader Styles

sonny3g
Collaborator
Collaborator

Trying to get back into .NET programming and I need to be able to have a user select existing a mleader and based on the mleaderstyle.add it to a selection set in C#.  So far, I can find filter based on MLEADER, but I cannot figure out how to further filter by mleaderstyle. 

 

Can anyone help get me pointed in the right direction?

 

Thanks.

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes
1,795 Views
8 Replies
Replies (8)
Message 2 of 9

_Tharwat
Advisor
Advisor

Hi,

 

As far as I know, to get a selection set of Multileader style filtered is something that is not possible but to iterate through the selection set is the working process for this task I believe.

 

Good luck.

0 Likes
Message 3 of 9

sonny3g
Collaborator
Collaborator

So, I cannot drill in and filter my selection set by multileader style.  However, as all of the multileaders I am going to work with MUST be located on a layer with a specific suffix I can further isolate the initial selection set to the specific multileaders I need.

 

The next issue is that I need to iterate through the selection set to make sure that it has MText.  If not, then go to the next item in the selection set.

 

If it does, then I need to read the first line of that MText content, get the layername, get the leader start and end points and create a new MultiLeader on a different layer with only the first line of text in the MText using a different Multileader style using the same start and end points.

 

I am struggling with how to drill into the object to first make sure it has MText and then to read the MText content.

 

All help is greatly appreciated.

 

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes
Message 4 of 9

_Tharwat
Advisor
Advisor

Hi,

 

Its been for a while since I have worked with C# but your aim of codes seems to be easy enough, so here is my attempt and basic codes for you to start with.

 

        public static void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TypedValue[] tv = { new TypedValue(0, "MULTILEADER") };
                PromptSelectionResult sel = ed.GetSelection(new SelectionFilter(tv));
                if (sel.Status == PromptStatus.OK)
                {
                    foreach (ObjectId obj in sel.Value.GetObjectIds())
                    {
                        MLeader mld = (MLeader)tr.GetObject(obj, OpenMode.ForRead);
                        MLeaderStyle mls = (MLeaderStyle)tr.GetObject(mld.MLeaderStyle, OpenMode.ForRead);
                        if (mls.Name == "Test" && mld.MText.Contents.Contains("\\P"))
                        {
                            ed.WriteMessage("\nMleader style object <Test> was selected and its has more than one line of texts");
                        }
                    }
                }
                tr.Commit();
            }
        }
0 Likes
Message 5 of 9

sonny3g
Collaborator
Collaborator

Thanks!! I am beginning to make some progress now.

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes
Message 6 of 9

_Tharwat
Advisor
Advisor

You are welcome.

Good luck.

0 Likes
Message 7 of 9

Juergen_Becker
Advocate
Advocate

This.little code using LINQ for searching MTexts in a drawing, what goes realy fast (6MB Drawing under 1 second).

What you have to do is: Replace MText in MLeader.

 

You dont Need any foreach, which is slow.

 

http://drive-cad-with-code.blogspot.de/2011/04/tips-of-using-linq-in-autocad-net.html

http://spiderinnet1.typepad.com/blog/2012/04/use-linq-to-filter-entities-in-autocad-net.html

https://wtertinek.com/2015/05/31/linq-and-the-autocad-net-api-part-4/

http://through-the-interface.typepad.com/through_the_interface/2013/01/filtering-lists-of-autocad-en...

 

 

using (Transaction m_Tr = m_DataBase.TransactionManager.StartTransaction())
{
  BlockTable m_BlockTable = (BlockTable)m_Tr.GetObject(m_DataBase.BlockTableId, OpenMode.ForRead);
  BlockTableRecord m_BlockTableRecord = 
    (BlockTableRecord)m_Tr.GetObject(m_BlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);

  var m_ObjectCollection = from m_ObjectId in m_BlockTableRecord.Cast<ObjectId>()
                           select new
                           {
                               m_DBObject = (DBObject)m_Tr.GetObject(m_ObjectId, OpenMode.ForRead, false)
                           };
                 
  IEnumerable<MText> m_dbMText = from m_OBJECT in m_ObjectCollection
                                 where (m_OBJECT.m_DBObject.GetType() == typeof(MText))
                                 select (MText)m_OBJECT.m_DBObject;

  m_dbMText = from m_Mtext in m_dbMText
              let Layer = m_ACADSettings.COSTextLayer
              where (m_Mtext.Layer == Layer)
              select (MText)m_Mtext;

  m_MTextList = m_dbMText.Cast<MText>().ToList();
  m_MTextList.ForEach(m_Text => m_ObjectIdList.Add(m_Text.ObjectId));
  m_Tr.Commit();
}

 

I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

Message 8 of 9

_Tharwat
Advisor
Advisor

Hi,

 

I guess you misunderstood what the OP wants.

They want to search for a specific Mleader Style Name and once found, then search its related Mtext string.

0 Likes
Message 9 of 9

Juergen_Becker
Advocate
Advocate

No, I think, I don't misunderstand it.

 

What about this:

 

using (Transaction m_Tr = m_DataBase.TransactionManager.StartTransaction())
{
  BlockTable m_BlockTable = (BlockTable)m_Tr.GetObject(m_DataBase.BlockTableId, OpenMode.ForRead);
  BlockTableRecord m_BlockTableRecord =
      (BlockTableRecord)m_Tr.GetObject(m_BlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);

  var m_ObjectCollection = from m_ObjectId in m_BlockTableRecord.Cast<ObjectId>()
                           select new
                           {
                               m_DBObject = (DBObject)m_Tr.GetObject(m_ObjectId, OpenMode.ForRead, false)
                           };

  IEnumerable<MLeader> m_dbMLeader = from m_OBJECT in m_ObjectCollection
                                     where (m_OBJECT.m_DBObject.GetType() == typeof(MLeader))
                                     select (MLeader)m_OBJECT.m_DBObject;

  m_dbMLeader = from m_MLeader in m_dbMLeader
                let StyleID = m_LeaderStyle.ObjectId
                where (m_MLeader.MLeaderStyle == StyleID)
                select (MLeader)m_MLeader;
List<MLeader> m_MLeaderList = null; m_MLeaderList = m_dbMLeader.Cast<MLeader>().ToList(); m_MLeaderList.ForEach(m_MLeader => m_ObjectIdList.Add(m_MLeader.ObjectId)); m_Tr.Commit(); }

This code searching for MLeaders with specific Style.

 

m_dbMLeader = from m_MLeader in m_dbMLeader
                let StyleID = m_LeaderStyle.ObjectId
                where (m_MLeader.MLeaderStyle == StyleID)
                select (MLeader)m_MLeader;

First you have to define what Style you want (m_LeaderStyle). Please Change the Where-Condition according to your need (Text etc.)

 

I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

0 Likes