If the "SetHatchPattern" method throws an "eInvalidInput" exception while creating a hatch using a CustomDefined hatch pattern, here are few points to check.
1) Is the pattern file that defines the custom hatch pattern named correctly ? The name of the hatch pattern and the pat file name should be the same.
2) Is the folder in which the pat file is placed, added to the AutoCAD support path ?
3) Is the line breaks in the pat file ok ? The easiest way to ensure that it is ok is to take an existing custom hatch pattern file and change the definitions to suit your requirements. AutoCAD is sensitive to line breaks in the pat file and "eInvalidInput" exception because of this can be hard to spot. You can find several online resources such as this to download a custom hatch pattern file.
Here is a sample code to try out the custom hatch pattern :
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptResult prHatchPatternName
= ed.GetString("\nEnter custom hatch pattern name : ");
if (prHatchPatternName.Status != PromptStatus.OK)
return;
string patName = prHatchPatternName.StringResult;
PromptPointResult ppr = ed.GetPoint("Pick insertion point: ");
if (ppr.Status != PromptStatus.OK)
return;
Point3d insertionPt = ppr.Value;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt
= tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord btr
= tr.GetObject( bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite
) as BlockTableRecord;
Point2d pt = new Point2d(insertionPt.X, insertionPt.Y);
Polyline plBox;
plBox = new Polyline(4);
plBox.Normal = Vector3d.ZAxis;
plBox.AddVertexAt(0, pt, 0.0, -1.0, -1.0);
plBox.AddVertexAt(1, new Point2d(pt.X + 10, pt.Y), 0.0, -1.0, -1.0);
plBox.AddVertexAt(2, new Point2d(pt.X + 10, pt.Y + 5), 0.0, -1.0, -1.0);
plBox.AddVertexAt(3, new Point2d(pt.X, pt.Y + 5), 0.0, -1.0, -1.0);
plBox.Closed = true;
ObjectId pLineId;
pLineId = btr.AppendEntity(plBox);
tr.AddNewlyCreatedDBObject(plBox, true);
ObjectIdCollection ObjIds = new ObjectIdCollection();
ObjIds.Add(pLineId);
Hatch hatchObj = new Hatch();
hatchObj.SetDatabaseDefaults();
Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
hatchObj.HatchObjectType = HatchObjectType.HatchObject;
hatchObj.Color
= Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
hatchObj.Normal = normal;
hatchObj.Elevation = 0.0;
hatchObj.SetHatchPattern(HatchPatternType.CustomDefined, patName);
btr.AppendEntity(hatchObj);
tr.AddNewlyCreatedDBObject(hatchObj, true);
hatchObj.Associative = true;
hatchObj.AppendLoop((int)HatchLoopTypes.Default, ObjIds);
hatchObj.EvaluateHatch(true);
tr.Commit();
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString() + Environment.NewLine);
}