Author Topic: Setting insertion point for a block  (Read 2105 times)

0 Members and 1 Guest are viewing this topic.

TJK44

  • Guest
Setting insertion point for a block
« on: October 27, 2011, 09:12:07 AM »
I having a slight issue when I am creating a block. After I get through naming the block then selection the entities for the block, I then ask for an insert point and return  the PromptPointResult. Then use this value for the new block reference.

Code: [Select]
Dim blockRefPointOptions As PromptPointOptions = New PromptPointOptions("Pick insertion point of BlockRef : ")
Dim blockRefPointResult As PromptPointResult = ed.GetPoint(blockRefPointOptions)
Dim pt As Point3d = blockRefPointResult.Value

Dim blockRef As BlockReference = New BlockReference(pt, newBlockDef.ObjectId)

However when after I select the insert point, it doesn't put the block where I clicked. It places it to the right of the point I selected. This picture I have attached illustrates what is happening. Within the properties box the position is correct where I clicked to choose the insertion point and that is also where the blue square is, but the block shows up far to the right.

Any help greatly appreciated.
Ted




BillZndl

  • Guest
Re: Setting insertion point for a block
« Reply #1 on: October 27, 2011, 10:03:21 AM »
Been a while since I worked with block references but I think you have to set you block table record "origin" to 0,0.

TJK44

  • Guest
Re: Setting insertion point for a block
« Reply #2 on: October 27, 2011, 11:01:20 AM »
I'm not sure you can set an origin for the block table record?

This is what I have for my block table record.

Code: [Select]
Dim curSpace As BlockTableRecord = trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite)
curSpace.AppendEntity(blockRef)

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Setting insertion point for a block
« Reply #3 on: October 27, 2011, 11:15:17 AM »
If the 'blue square' is where you clicked then I guess it is inserting it correctly.
 
I think your problem is how you are adding your entites to the definition.
 
If you open the block in block editor the entites will probably be 'off to the right' from the orgin

TJK44

  • Guest
Re: Setting insertion point for a block
« Reply #4 on: October 27, 2011, 11:46:05 AM »
Yes, where the blue square is placed is where I am clicking. I went to the block editor, did not see any place where it said 'off to left'. I am using ed.GetSelection to select which entities I want in the block. Here is the code I am using to loop through and assign each entity to the block table record.

Code: [Select]
Dim oidc = New ObjectIdCollection()
                        For Each id In res.Value.GetObjectIds()
                            If id.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(Entity))) Then
                                oidc.Add(id)
                            End If
                        Next
                        newBlockDef.AssumeOwnershipOf(oidc)

Thank you,
Ted

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Setting insertion point for a block
« Reply #5 on: October 27, 2011, 12:05:14 PM »
Sorry I was not being very clear, what I meant is the entites are a placed a certin distance from the BlockTableRecord's Orgin, and as Bill mentioned there is a BlockTableRecord.Origin  property.
 
I can't think of a way or what correct words to use but '''the objects need to be moved to BlockTableRecord orgin'''.
 
 

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Setting insertion point for a block
« Reply #6 on: October 27, 2011, 03:36:24 PM »
Hi,

Here's a quick and dirty C# command example that mimics the BLOCK native command.

Code: [Select]
[CommandMethod("Test")]
public void Test()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

// User inputs
PromptStringOptions pso = new PromptStringOptions("\nBlock name: ");
pso.AllowSpaces = false;
PromptResult pr = ed.GetString(pso);
if (pr.Status != PromptStatus.OK) return;
PromptPointResult ppr = ed.GetPoint("\nInsertion point: ");
if (ppr.Status != PromptStatus.OK) return;
PromptSelectionResult psr = ed.GetSelection();
if (psr.Status != PromptStatus.OK) return;

// Datas
ObjectIdCollection oid = new ObjectIdCollection(psr.Value.GetObjectIds());
Point3d pt = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);

using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Create a new block record
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord btr = new BlockTableRecord();
bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
btr.Name = pr.StringResult;

// Displace all selected entities from picked point to  origin
Vector3d disp = pt.GetVectorTo(Point3d.Origin);
foreach (ObjectId id in oid)
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
ent.TransformBy(Matrix3d.Displacement(disp));
}

// Add entities to the block record
btr.AssumeOwnershipOf(oid);

// Insert the newly created block at picked point
BlockTableRecord space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockReference br = new BlockReference(pt, btr.ObjectId);
space.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);

tr.Commit();
}
}
Speaking English as a French Frog