.NET

Reply
Active Member
JuramemO
Posts: 10
Registered: ‎10-29-2013
Message 1 of 7 (359 Views)
Accepted Solution

Refresh drawing after block modivication

359 Views, 6 Replies
11-26-2013 08:41 AM

 

Dear all!

 

My code mirrors a selection by either the x-axis or the y-axis.

 

It is working fine.

 

Exept, the drawing shows the result, when I move my cursor to the active dwg.

 

How can I refresh my drawing, direct after modification of a block?

 

Any sugestions to a clean solution?

 

Thank you!

 

I use the code below to mirror a selected block by a button on a docking pallette:

 

private void btn_Vertical_Mirror_Click(object sender, EventArgs e)
{
	(new Modifikation()).Mirror("v");
}

 

the code, which is called is:

public void Mirror(String axis = "")
{
	//---------------------------------------------------------------------------------------------------------------------------------------
	Document dwg = Application.DocumentManager.MdiActiveDocument;
	Database db = dwg.Database;
	Editor edt = dwg.Editor;
	//---------------------------------------------------------------------------------------------------------------------------------------
	try
	{
		Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
		PromptSelectionResult sel = edt.SelectImplied();
		//-----------------------------------------------------------------------------------------------------------------------------------
		if (sel.Status == PromptStatus.Error)
		{
			//Selection filter
			TypedValue[] tv = new TypedValue[] { new TypedValue( (int)DxfCode.Start, "INSERT") };
			SelectionFilter sf = new SelectionFilter(tv);
			PromptSelectionOptions pso = new PromptSelectionOptions();

			pso.MessageForAdding = "\nSelect block";
			sel = edt.GetSelection(pso, sf);

			if (sel.Status != PromptStatus.OK)//
			{
				edt.WriteMessage("\n#ERROR! Selection failed\n");
				return;
			}//if
		}
		//-----------------------------------------------------------------------------------------------------------------------------------
		while (axis == "")
		{
			axis = edt.GetString("Axis ('H'orizontaly oder 'V'erticaly)").StringResult.ToUpper();
			if (axis.StartsWith("H")) { axis = "h"; }
			else if (axis.StartsWith("V")) { axis = "v"; }
			else { axis = ""; }
		}
		//-----------------------------------------------------------------------------------------------------------------------------------
		using (Transaction tr = db.TransactionManager.StartTransaction())
		{
			using (DocumentLock lk = dwg.LockDocument())
			{
				for (int i = 0; i < sel.Value.Count; i++)
				{
					if (sel.Value[i].ObjectId.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(BlockReference))))// Blöcke werden der Auswahl entnommen
					{
						BlockReference blk_ref = (BlockReference)tr.GetObject(sel.Value[i].ObjectId, OpenMode.ForWrite);

						//-------------------------------------------------------------------------------------------------------------------
						if (axis == "h")
						{
							Matrix3d spgl = Matrix3d.Mirroring(
								new Line3d(
									blk_ref.Position,
									new Point3d(
										blk_ref.Position.X + 1,
										blk_ref.Position.Y, 0)));
							blk_ref.TransformBy(spgl);
						}
						//-------------------------------------------------------------------------------------------------------------------
						else if (axis == "v")
						{
							Matrix3d spgl = Matrix3d.Mirroring(
								new Line3d(
									blk_ref.Position,
									new Point3d(
										blk_ref.Position.X,
										blk_ref.Position.Y + 1, 0)));
							blk_ref.TransformBy(spgl);
						}
						//-------------------------------------------------------------------------------------------------------------------
					}//if
				}// for
			}// using DocumentLock
			tr.Commit();
		}// using Transaction
		edt.WriteMessage("\nOkay\n");
	}// try
	//---------------------------------------------------------------------------------------------------------------------------------------
	catch { edt.WriteMessage("\n#Error!\n"); }
	//---------------------------------------------------------------------------------------------------------------------------------------
	finally { edt.WriteMessage("\nDone\n"); }
}// public void

 

 

 

try by adding this line at the end of your code

 

'' Regenerate the drawing
edt.Regen()

 http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/files/WS1a9193826455f5ff2566ffd511ff6f8c7ca-42db.htm

Valued Mentor
Ajilal.Vijayan
Posts: 449
Registered: ‎01-12-2008
Message 2 of 7 (351 Views)

Re: Refresh drawing after block modivication

11-26-2013 09:06 AM in reply to: JuramemO

try by adding this line at the end of your code

 

'' Regenerate the drawing
edt.Regen()

 http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/files/WS1a9193826455f5f...

Active Member
JuramemO
Posts: 10
Registered: ‎10-29-2013
Message 3 of 7 (339 Views)

Re: Refresh drawing after block modivication

11-26-2013 09:56 AM in reply to: Ajilal.Vijayan
Hallo and thank you for your fast and competent reply!

Thank's, once more!
ADN Support Specialist
StephenPreston
Posts: 422
Registered: ‎05-22-2006
Message 4 of 7 (308 Views)

Re: Refresh drawing after block modivication

11-26-2013 02:02 PM in reply to: JuramemO

Calling Regen can be a tad slow for a large drawing. When I've modified a BlockTableRecord, I prefer to iterate through all the BlockReferences for that BlockTableRecord and call RecordGraphicsModified(0 on each one. Something like this:

 

   http://adndevblog.typepad.com/autocad/2012/05/redefining-a-block.html

 

Its personal choice really - just mentioning this for others who may find this post in the future.

Cheers,

Stephen Preston
Autodesk Developer Network
Active Member
JuramemO
Posts: 10
Registered: ‎10-29-2013
Message 5 of 7 (286 Views)

Re: Refresh drawing after block modivication

11-27-2013 10:49 AM in reply to: StephenPreston

 

Hi Stephen!

 

Some kind of loop was also my first idea.

 

I tryed to refresh my drawing by using the code below.

It's a function, which is called after closing the transaction.

 

private void RefreshBlocks()
{
  //-------------------------
  Document dwg = Application.DocumentManager.MdiActiveDocument;
  Database db = dwg.Database;
  Editor edt = dwg.Editor;
  //-------------------------
  Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
  PromptSelectionResult sel = edt.SelectImplied();
  if (sel.Status == PromptStatus.Error) { sel = edt.SelectAll(); }
  //-------------------------
  try
  {
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
      using (DocumentLock lk = dwg.LockDocument())
      {
        for (int i = 0; i < sel.Value.Count; i++)// Loop objects
        {
          if (sel.Value[i].ObjectId.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(BlockReference))))// proceed only blocks
          {
            BlockReference blk_ref = (BlockReference)tr.GetObject(sel.Value[i].ObjectId, OpenMode.ForWrite);
            blk_ref.RecordGraphicsModified(true);
          }// if
        }// for
      }// using DocumentLock 
      tr.Commit();
    }// using Transaction 
  }// try
  //-------------------------
  catch { }
  //-------------------------
}

 

But something is wrong. The changes on the block are not shown, until i set the focus to the drawing.

By calling < regen > everything goes well.

 

Can you point your finger to the mistake or error of the code?

 

Thank you!

 

*Expert Elite*
_gile
Posts: 2,125
Registered: ‎04-29-2006
Message 6 of 7 (279 Views)

Re: Refresh drawing after block modivication

11-27-2013 12:26 PM in reply to: JuramemO

Hi,

 

You can try adding:

db.TransactionManager.QueueForGraphicsFlush();

 after each:

blk_ref.TransformBy(spgl);

 

Gilles Chanteau
Active Member
JuramemO
Posts: 10
Registered: ‎10-29-2013
Message 7 of 7 (268 Views)

Re: Refresh drawing after block modivication

11-28-2013 01:11 AM in reply to: JuramemO

 

After playing around I finaly have a (partial) solution.

 

I insered the following rows to my code:

using (Transaction tr = db.TransactionManager.StartTransaction())
{
  using (DocumentLock lk = dwg.LockDocument())
  {
    dwg.TransactionManager.EnableGraphicsFlush(true);// new
	
	// Code for modification of the block(s)
	
    db.TransactionManager.QueueForGraphicsFlush();// new
dwg.TransactionManager.FlushGraphics();// new
}// using DocumentLock
tr.Commit();
}// using Transaction

 

BUT!! It showes some strainge behaviour:

 

 

Rotating of blocks is working fine.

 

Mirroring of blocks is only working, by selecting the blocks by a < edt.GetSelection(pso, sf) > command.

Using  < edt.SelectImplied(); >, the updating of the drawing fails (must be triggered by activating by the operator)

I can overcome this, by using the code below.

But it looks not nice to me.

PromptSelectionResult sel = edt.SelectImplied();
if (sel.Status == PromptStatus.OK)
{
  ObjectId[] sel_tmp = new ObjectId[sel.Value.Count];
  for (int i = 0; i < sel.Value.Count; i++) temp_sel[i] = sel.Value[i].ObjectId;
  edt.SetImpliedSelection(new ObjectId[0]);
  edt.SetImpliedSelection(sel_tmp);
}// if 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.