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

identify the presence of the block in a particular layout

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
spu
Advocate
618 Views, 2 Replies

identify the presence of the block in a particular layout

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

2 REPLIES 2
Message 2 of 3
Alexander.Rivilis
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.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 3
spu
Advocate
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.

 

 

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost