TheSwamp
Code Red => .NET => Topic started by: Bernd on June 12, 2017, 05:03:52 AM
-
I have difficulties getting the hatch angle of e.g. ANSI31 drawn correctly. I want to align the pattern to the longest side of the MPolygon.
Therefore I use the following code:
mPolygon.SetPattern(HatchPatternType.PreDefined, muster);
mPolygon.Color = umringFarbe;
mPolygon.PatternColor = fuellFarbe;
mPolygon.Layer = layer;
if (muster.ToUpperInvariant() != "SOLID")
{
double winkel = schraffurWinkel + laengsteLinie.Direction.Angle;
if (winkel > 2 * Math.PI)
winkel -= Math.PI;
if (winkel >= 0 && winkel < Math.PI)
mPolygon.PatternAngle = winkel;
else
mPolygon.PatternAngle = winkel - Math.PI;
mPolygon.PatternScale = schraffurFaktor == 0 ? 1 : schraffurFaktor;
}
muster is the name of the pattern - in this case ANSI31 - , laengsteLinie the longest side of the MPolygon.
Later on the MPolygon is added like this:
try
{
ObjectId blockTableId = dwg.BlockTableId;
var bt = (BlockTable)trans.GetObject(blockTableId, OpenMode.ForRead);
ObjectId modelSpace = bt[BlockTableRecord.ModelSpace];
var btr = (BlockTableRecord)trans.GetObject(modelSpace, OpenMode.ForWrite);
btr.AppendEntity(mPolygon);
handle = mPolygon.Handle.Value;
mPolygon.RecordGraphicsModified(true);
trans.AddNewlyCreatedDBObject(mPolygon, true);
trans.Commit();
return handle;
}
Everything works as expected except the hatch angle. The hatch is being drawn in a 45° angle, which is how the hatch is defined, no mattern how the surrounding rectangle (or whatever the shape is) is rotated. Looking at the MPolygon's properties I see, that there is actually a different angle defined, the one that I programatically defined. And as soon as I change some other property, e.g. the spacing, the MPoylgon is being redrawn, this time with the angle applied. How come?
Any ideas are highly appreciated...
Thanks, Bernd
-
I do something similar but in 2 steps.
Second step:
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Hatch h = tr.GetObject(id, OpenMode.ForWrite) as Hatch;
h.PatternAngle = ang;
h.PatternScale = scale;
h.SetHatchPattern(HatchPatternType.PreDefined, sHatch);
tr.Commit();
}
I wonder if .SetHatchPattern must be after setting the angle
-
Bingo!
Looks like MPolygon.SetPattern() has to be the last call after the hatch's other properties have been set.
Thanks a lot for that hint!