.NET

Reply
Distinguished Contributor spu
Distinguished Contributor
spu
Posts: 141
Registered: ‎02-21-2007
Message 1 of 3 (280 Views)
Accepted Solution

identify the presence of the block in a particular layout

280 Views, 2 Replies
12-25-2012 11:55 PM

Hi,

 

I have to insert a block in a paerspace layout if it is not already present in that layout.

I tried the following function to identify the presence of the block.

But it returns true even if the block is not present in the particular layout but is present in another layout.

 

private bool checkBlockPresence()
        {
            try
            {
                Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                Autodesk.AutoCAD.DatabaseServices.TypedValue[] acTypValAr;
                acTypValAr = new Autodesk.AutoCAD.DatabaseServices.TypedValue[1];
                //acTypValAr.SetValue(new Autodesk.AutoCAD.DatabaseServices.TypedValue((int)Autodesk.AutoCAD.DatabaseServices.DxfCode.Start, "INSERT"), 0);
                acTypValAr.SetValue(new Autodesk.AutoCAD.DatabaseServices.TypedValue((int)Autodesk.AutoCAD.DatabaseServices.DxfCode.BlockName, "BPC_PrjDrg_Logo"), 0);
                Autodesk.AutoCAD.EditorInput.SelectionFilter sf = new Autodesk.AutoCAD.EditorInput.SelectionFilter(acTypValAr);
                Autodesk.AutoCAD.EditorInput.PromptSelectionResult selPrompt = ed.SelectAll(sf);
                if (selPrompt.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                {
                    Autodesk.AutoCAD.EditorInput.SelectionSet acSet = selPrompt.Value;
                    if (acSet.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error while checkBlockPresence: Type" + ex.GetType() + "  Message:" + ex.Message);
            }
                return false;
        }

 

Also I am using the following function to make the particular layout active before calling the above function.

 

        public void SwitchTo(string Layout)
        {
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Autodesk.AutoCAD.DatabaseServices.Database acCurDb = acDoc.Database;
            using (Autodesk.AutoCAD.ApplicationServices.DocumentLock doclock = acDoc.LockDocument())
            {
                using (Autodesk.AutoCAD.DatabaseServices.Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    try
                    {
                        Autodesk.AutoCAD.DatabaseServices.LayoutManager acLayoutMgr;
                        acLayoutMgr = Autodesk.AutoCAD.DatabaseServices.LayoutManager.Current;
                        acLayoutMgr.CurrentLayout = Layout;

                        //  System.Windows.Forms.MessageBox.Show("Current Layout = " + acLayoutMgr.CurrentLayout);
                        acTrans.Commit();
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Exception while changing to " + Layout + "\n Error:" + ex.Message);
                    }

                }
            }
        }

 

So my code is similer to

 

SwithTo("A3")

if(checkBlockPresence() == false)  // this always returns true.

{

---------

}

 

 

 

Regards,

Shijith

private bool checkBlockPresence(string BlockName, string LayoutName)
{
try {
Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Autodesk.AutoCAD.DatabaseServices.TypedValue[] acTypValAr;
acTypValAr = new Autodesk.AutoCAD.DatabaseServices.TypedValue[2];
acTypValAr.SetValue(new Autodesk.AutoCAD.DatabaseServices.TypedValue((int)Autodesk.AutoCAD.DatabaseServices.DxfCode.BlockName, BlockName), 0);
 acTypValAr.SetValue(new Autodesk.AutoCAD.DatabaseServices.TypedValue((int)Autodesk.AutoCAD.DatabaseServices.DxfCode.LayoutName, LayoutName), 1);
Autodesk.AutoCAD.EditorInput.SelectionFilter sf = new Autodesk.AutoCAD.EditorInput.SelectionFilter(acTypValAr);
Autodesk.AutoCAD.EditorInput.PromptSelectionResult selPrompt = ed.SelectAll(sf);
if (selPrompt.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK) {
Autodesk.AutoCAD.EditorInput.SelectionSet acSet = selPrompt.Value;
if (acSet.Count > 0) return true;
else return false;
}
}
catch (Exception ex) {
System.Windows.Forms.MessageBox.Show("Error while checkBlockPresence: Type" + ex.GetType() + " Message:" + ex.Message);
}
return false;
}

I hope this code help you.

Moderator
Alexander.Rivilis
Posts: 1,449
Registered: ‎04-09-2008
Message 2 of 3 (267 Views)

Re: identify the presence of the block in a particular layout

12-26-2012 01:39 AM in reply to: spu
private bool checkBlockPresence(string BlockName, string LayoutName)
{
  try {
    Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
    Autodesk.AutoCAD.DatabaseServices.TypedValue[] acTypValAr;
    acTypValAr = new Autodesk.AutoCAD.DatabaseServices.TypedValue[2];
    acTypValAr.SetValue(new Autodesk.AutoCAD.DatabaseServices.TypedValue((int)Autodesk.AutoCAD.DatabaseServices.DxfCode.BlockName, BlockName), 0);
    acTypValAr.SetValue(new Autodesk.AutoCAD.DatabaseServices.TypedValue((int)Autodesk.AutoCAD.DatabaseServices.DxfCode.LayoutName, LayoutName), 1);
    Autodesk.AutoCAD.EditorInput.SelectionFilter sf = new Autodesk.AutoCAD.EditorInput.SelectionFilter(acTypValAr);
    Autodesk.AutoCAD.EditorInput.PromptSelectionResult selPrompt = ed.SelectAll(sf);
    if (selPrompt.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK) {
      Autodesk.AutoCAD.EditorInput.SelectionSet acSet = selPrompt.Value;
      if (acSet.Count > 0) return true;
                else       return false;
    }
  }
  catch (Exception ex) {
    System.Windows.Forms.MessageBox.Show("Error while checkBlockPresence: Type" + ex.GetType() + "  Message:" + ex.Message);
  }
  return false;
}

I hope this code help you.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor spu
Distinguished Contributor
spu
Posts: 141
Registered: ‎02-21-2007
Message 3 of 3 (261 Views)

Re: identify the presence of the block in a particular layout

12-26-2012 01:54 AM in reply to: Alexander.Rivilis

Hi,

 

Thank you so much for the reply. It worked perfectly. I didn't thought about placing layout in filter itself.

Regrads,

Shijith.

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!