Defining a custom hatch pattern in code?

Defining a custom hatch pattern in code?

Anonymous
Not applicable
1,319 Views
3 Replies
Message 1 of 4

Defining a custom hatch pattern in code?

Anonymous
Not applicable

I need to be able to define a custom hatch pattern at runtime. Nothing too complicated, just perpendicular lines but i need to be able to specify the spacing between the lines based on user input.

 

I see that you can do:  hatch.SetHatchPattern(HatchPatternType.CustomDefined, "customHatch");

 

But I'm not entirely sure how to proceed from there. The only example I have been able to find online is here - https://forums.autodesk.com/t5/net/custom-hatch-pattern-in-code/td-p/6810422

 

In this example he's writing the hatch data to a .pat file in a specific folder on the hard drive. Is it necessary to write a file to the hard drive if it's a hatch pattern that might only be used once? Or can it be entirely defined in code? And if it does need to be written to the hard drive, in the example linked above he's hard coding the file path based on a specific version of Autocad. How would one get the appropriate path for whatever version of Autocad is currently running rather than specifying a certain version?

 

Thanks!

0 Likes
Accepted solutions (1)
1,320 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

I never tried to programmatically create custom Hatch Pattern, but guess AutoCAD can only use/load custom Hatch Pattern from *.pat file, thus, if you need to use code-created custom pattern only when your code runs, you need to create the *.pat file first. Of course you may want to clean it up after use, say, what AutoCAd session closes, you delete in IExtensionApplication.Terminate().

 

As for the *.pat file location, I think, it must be one of the support paths AutoCAD would search automatically when trying to load the custom pattern. In the post you referred, the path was "hard-coded" to ...\users\[username]\AppData\Roaming\Autodesk......\Support\", which is one of default support paths set by AutoCAD when a user runs AutoCAD in a computer the first time. However, that path may not be 100% guaranteed to be one of the AutoCAD support paths for some odd reasons. So, I'd get all the support paths, find one that is write-able to the current user (the code could simply try to write text file to the folder, if denied, try next support path...). This way, the path is not really "hard coded" and would not affected by AutoCAD version/installation.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

Anonymous
Not applicable
Accepted solution

Well I got it working. I still don't know if its possible to do it without saving a .pat file to the hard drive so I just made some modifications to the example I linked to above.

 

I remembered that if you don't specify a path the file will be saved in whatever directory my .dll is running from so there's really no need to specify the long Autocad path. The directory in use is already in the user's support files search paths in order to run this command in the first place so it will be fine to store the file there. This way the path won't break if they're using a new version of Autocad with some different path 5 years from now.

 

And i used a StreamWriter that will overwrite any existing file named CustomHatch.pat so I'm not cluttering the directory with a new file every time this gets called. It would still be nice to know if it's possible to do this without the .pat file but at least it's working now so I'm going to call this one solved.

0 Likes
Message 4 of 4

norman.yuan
Mentor
Mentor

Well, the DLL location, even being one of the support paths, may not necessarily be write-able to the current AutoCAD user. So, you might as well have code to handle it in a way that it can find a different path in support paths that is write-able.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes