i have a .dwg fille with some block reference.
I want to fill color for block reference, but when I debug to hatch.appendloop function, I get message eInvalidInput, and block don’t fill color.
This is my code:
[CommandMethod("FillColor")]
public void FillColor()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed =
doc.Editor;
Database db =
HostApplicationServices.WorkingDatabase;
Transaction tr =
db.TransactionManager.StartTransaction();
// Start the transaction
try
{
// Build a filter list so that only
// block references are selected
TypedValue[] filList = new TypedValue[1] {
new TypedValue((int)DxfCode.Start, "INSERT")
};
SelectionFilter filter =
new SelectionFilter(filList);
PromptSelectionOptions opts =
new PromptSelectionOptions();
opts.MessageForAdding = "Select block references: ";
PromptSelectionResult res =
ed.GetSelection(opts, filter);
// Do nothing if selection is unsuccessful
if (res.Status != PromptStatus.OK)
return;
SelectionSet selSet = res.Value;
ObjectId[] idArray = selSet.GetObjectIds();
foreach (ObjectId blkId in idArray)
{
BlockTable bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
);
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.PaperSpace],
OpenMode.ForWrite
);
Hatch hat = new Hatch();
hat.SetDatabaseDefaults();
hat.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
hat.Color = Color.FromRgb(0, 0, 255);
// Add the hatch to the model space
// and the transaction
ObjectId hatId = btr.AppendEntity(hat);
tr.AddNewlyCreatedDBObject(hat, true);
// Add the hatch loop and complete the hatch
ObjectIdCollection ids = new ObjectIdCollection();
ids.Add(blkId);
hat.Associative = true;
hat.AppendLoop(HatchLoopTypes.Default, ids); // error here....
hat.EvaluateHatch(true);
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(("Exception: " + ex.Message));
}
finally
{
tr.Dispose();
}
}
Please help me, if you can!
Hi,
Not sure, but I think that a HatchLoop should be a curve derived object like a polyline, and you are using a block reference which it's not.
Gaston Nunez
Can't find what you're looking for? Ask the community or share your knowledge.