Author Topic: Database.Insert and Hatch in Dynamic Block  (Read 8208 times)

0 Members and 1 Guest are viewing this topic.

bargool

  • Guest
Database.Insert and Hatch in Dynamic Block
« on: February 13, 2013, 06:16:01 AM »
Hello!
When I insert dynamic block into opened document and changing the dynamic properties, hatch in the block updates.
But when I use database of non-opened document it doesn't. The hatch updates after opening, saving and closing block in block editor, but this is not the solution!

Does somebody have expierence to solve second-variant problem?

Code snippet to insert block via database, some variables are implemented outside:
Code - C#: [Select]
  1. db = new Database(true, true);
  2. using (Database blockdb = new Database(false, true))
  3. {
  4.         blockdb.ReadDwgFile(sourcepath, FileOpenMode.OpenForReadAndAllShare, false, "");
  5.         db.Insert(blockname, blockdb, true);
  6. }
  7. using (Transaction tr = db.TransactionManager.StartTransaction())
  8. {
  9.         BlockTable bt = db.BlockTableId.GetObject<BlockTable>();
  10.         BlockTableRecord model =
  11.                 bt[BlockTableRecord.ModelSpace].GetObject<BlockTableRecord>(OpenMode.ForWrite);
  12.         BlockReference bref = new BlockReference(pos, bt[blockname]);
  13.         bref.SetDatabaseDefaults();
  14.         model.AppendEntity(bref);
  15.         tr.AddNewlyCreatedDBObject(bref, true);
  16.         // If hatch is in the block - it does not change after next strings of code.
  17.         using (DynamicBlockReferencePropertyCollection pc = bref.DynamicBlockReferencePropertyCollection)
  18.         {
  19.                 foreach (KeyValuePair<string, string> kvp in parameters) // parameters - dictionary of parameter names and values
  20.                 {
  21.                         DynamicBlockReferenceProperty prop = pc
  22.                                 .Cast<DynamicBlockReferenceProperty>()
  23.                                 .First(n => n.PropertyName == kvp.Key);
  24.                         if (prop.PropertyTypeCode == (short)DynamicPropertyTypes.Distance)
  25.                         {
  26.                                 prop.Value = double.Parse(kvp.Value, CultureInfo.InvariantCulture);
  27.                         }
  28.                        
  29.                         if (prop.PropertyTypeCode == (short)DynamicPropertyTypes.Visibility)
  30.                         {
  31.                                 object val = prop.GetAllowedValues()
  32.                                         .First(n => n.ToString() == kvp.Value);
  33.                                 prop.Value = val;
  34.                         }
  35.                 }
  36.         }
  37.         tr.Commit();
  38. }
  39. db.SaveAs(path, true, DwgVersion.Current, drawing.db.SecurityParameters);
  40. db.Dispose();

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Database.Insert and Hatch in Dynamic Block
« Reply #1 on: February 13, 2013, 10:21:47 AM »
I ran into a similar situation where setting HostApplicationServices.WorkingDatabase to the database I was updating attribute references in.

TheMaster

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #2 on: February 13, 2013, 02:51:20 PM »
Hello!
When I insert dynamic block into opened document and changing the dynamic properties, hatch in the block updates.
But when I use database of non-opened document it doesn't. The hatch updates after opening, saving and closing block in block editor, but this is not the solution!

Does somebody have expierence to solve second-variant problem?


Have you tried calling the BlockReference's ResetBlock() method?

Drafter X

  • Swamp Rat
  • Posts: 578
Re: Database.Insert and Hatch in Dynamic Block
« Reply #3 on: February 13, 2013, 04:11:28 PM »
seen sim problems before, I think the hatch is redrawn when the attributes are modified via gui and if its done directly it doesn't update, not sure though. reset block might set them to default visibility states and such wont it?
CadJockey Militia Commander

TheMaster

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #4 on: February 13, 2013, 08:06:37 PM »
Hello!
When I insert dynamic block into opened document and changing the dynamic properties, hatch in the block updates.
But when I use database of non-opened document it doesn't. The hatch updates after opening, saving and closing block in block editor, but this is not the solution!

Does somebody have expierence to solve second-variant problem?


Actually, I think I have the wrong API in mind (ResetBlock). It doesn't do what you need.

The one you should try is BlockTableRecord.UpdateAnonymousBlocks(), but that will update all dynamic block references. I don't see any other API that would solve your problem.

bargool

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #5 on: February 14, 2013, 01:08:13 AM »
Actually, I think I have the wrong API in mind (ResetBlock). It doesn't do what you need.

The one you should try is BlockTableRecord.UpdateAnonymousBlocks(), but that will update all dynamic block references. I don't see any other API that would solve your problem.
Yes, ResetBlock is not what I need.
I tryed BlockTableRecord.UpdateAnonymousBlocks(), doesn't help. I hoped that somebody found some trick  :-)
the hatch is redrawn when the attributes are modified via gui and if its done directly it doesn't update
Yes, I think something like this happens.

Drafter X

  • Swamp Rat
  • Posts: 578
Re: Database.Insert and Hatch in Dynamic Block
« Reply #6 on: February 14, 2013, 06:30:32 AM »
that may be a good use for that autocad console version maybe?
CadJockey Militia Commander

bargool

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #7 on: February 14, 2013, 06:32:36 AM »
that may be a good use for that autocad console version maybe?
I have to support AutoCADs from 2010

Drafter X

  • Swamp Rat
  • Posts: 578
Re: Database.Insert and Hatch in Dynamic Block
« Reply #8 on: February 14, 2013, 06:34:43 AM »
hmmm....
anyway you can flag the blocks and trigger a redraw when acad is opened so that autocad redraws it then instead?
CadJockey Militia Commander

TheMaster

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #9 on: February 14, 2013, 07:02:07 AM »
Actually, I think I have the wrong API in mind (ResetBlock). It doesn't do what you need.

The one you should try is BlockTableRecord.UpdateAnonymousBlocks(), but that will update all dynamic block references. I don't see any other API that would solve your problem.
Yes, ResetBlock is not what I need.
I tryed BlockTableRecord.UpdateAnonymousBlocks(), doesn't help. I hoped that somebody found some trick  :-)
the hatch is redrawn when the attributes are modified via gui and if its done directly it doesn't update
Yes, I think something like this happens.

When you clone a dynamic block reference, the referenced anonymous block must be getting cloned too, and this is what is most-likely not being hooked up to the dynamic block (although it should be).

The easiest solution is of course, to just recreate the dynamic block reference using the properties and insertion parameters of the cloned one, but other than that, you would need to look into what is happening in the deep-clone process to get an aswer.

bargool

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #10 on: February 14, 2013, 08:20:50 AM »
anyway you can flag the blocks and trigger a redraw when acad is opened so that autocad redraws it then instead?
What do you mean? Always look if there is some flagged block in opening drawing, and redraw it? But if the drawing will be opened without my program? Or there is some property in BlockReference (or in some of parent classes) that tells AutoCAD to recalculate the Object?
When you clone a dynamic block reference, the referenced anonymous block must be getting cloned too, and this is what is most-likely not being hooked up to the dynamic block (although it should be).

The easiest solution is of course, to just recreate the dynamic block reference using the properties and insertion parameters of the cloned one, but other than that, you would need to look into what is happening in the deep-clone process to get an aswer.
No, I import model space of another drawing, that contains dynamic block definition. Then I insert its BlockReference and change dynamic parameters. As result I have modified BlockReference with non-modified hatch. That is if I do this with database without opening drawing.
And that is interesting: If I will open this block via block editor, Autocad recalculates the hatch. Look to the illustration on youtube http://www.youtube.com/watch?v=3zcjUGdVNiM

If I open drawing in Acad (DocumentManager.Open()), and make all operations, I have good result. But I don't want to open this drawings.
« Last Edit: February 14, 2013, 08:29:57 AM by bargool »

TheMaster

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #11 on: February 21, 2013, 03:53:50 AM »
Have you tried setting HostApplicationServices.WorkingDatabase to the database that you are working with when you insert the dynamic block reference and set its properties?

bargool

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #12 on: February 21, 2013, 08:27:33 AM »
Have you tried setting HostApplicationServices.WorkingDatabase to the database that you are working with when you insert the dynamic block reference and set its properties?
Yep.
Hmm.. can Hatch.EvaluateHatch() help?

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Database.Insert and Hatch in Dynamic Block
« Reply #13 on: February 21, 2013, 01:29:33 PM »
Can you post the block so I have something to play with

TheMaster

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #14 on: February 21, 2013, 03:28:45 PM »
Have you tried setting HostApplicationServices.WorkingDatabase to the database that you are working with when you insert the dynamic block reference and set its properties?
Yep.
Hmm.. can Hatch.EvaluateHatch() help?

Possibly, but it must be done in the definition of the anonymous block referenced by the insertion.  It shouldn't be necessary, but it seems that hatching has some sort of dependence on the drawing editor.

bargool

  • Guest
Re: Database.Insert and Hatch in Dynamic Block
« Reply #15 on: February 21, 2013, 04:06:00 PM »
Can you post the block so I have something to play with
You are welcome. See attachment
Hmm.. can Hatch.EvaluateHatch() help?
Possibly, but it must be done in the definition of the anonymous block referenced by the insertion.  It shouldn't be necessary, but it seems that hatching has some sort of dependence on the drawing editor.
You are right. It's dependent.
Quote from arxdoc from ObjectARX SDK
Quote
After defining the hatch boundary and specifying the hatch pattern and style, the application must elaborate the hatch lines or solid fill for display. The Hatch class implementation maintains the computed hatch lines and solid fill to support WorldDraw() and ViewportDraw() methods for hatch entity display. However, the computed hatch lines and solid fill are not saved with the drawing or DXF files for file size efficiency. Instead, AutoCAD recomputes the hatch lines or solid fill when a DWG or DXF file is opened by AutoCAD.
However I tried and, of course, this didn't help.
Anyway, I need to create Layouts in new files, so I'm creating that files via DocumentManager now.