Jump to content

create transparent hatch with Python


Recommended Posts

Posted
import traceback
from pyrx import Ap, Db, Ed, Ge

_index = 1

def makeBoundary(db: Db.Database, pnt: Ge.Point3d, tr: Db.Transparency):
    ids = []
    plines = Ed.Editor.traceBoundary(pnt, False)
    for pline in plines:
        pline.setColorIndex(_index) # Uses the global integer
        pline.setTransparency(tr)
        ids.append(db.addToModelspace(pline))
    return ids

@Ap.Command()
def doit():
    global _index
    try:
        db = Db.curDb()
        tr = Db.Transparency(0.5) #50%
        
        ps, pnt = Ed.Editor.getPoint("\nSelect internal point: ")
        if ps != Ed.PromptStatus.eOk:
            return
        
        # 1. Generate the boundary polylines
        ids = makeBoundary(db, pnt, tr)
        if not ids:
            print("\nNo boundary found.")
            return
        
        # 2. Create and configure the Hatch object
        hat = Db.Hatch()
        hat.setPattern(Db.HatchPatternType.kPreDefined, "SOLID")
        hat.setColorIndex(_index)
        hat.setTransparency(tr)
        
        # 3. Add hatch to model space BEFORE appending loops (AutoCAD requirement)
        db.addToModelspace(hat)
        
        # 4. Establish associativity and evaluate
        hat.setAssociative(True)
        hat.appendLoop(Db.HatchLoopType.kDefault, ids)
        hat.evaluateHatch(True)
        
        print(f"\nCreated boundary and hatch with Color Index: {_index}")
        
        # 5. Increment color index (Cycle back to 1 if it exceeds standard ACI 255)
        _index += 1
        if _index > 255:
            _index = 1
            
    except Exception as e:
        print("Error encountered:")
        traceback.print_exc()

 

hatch.png.fd00f46c70c699510b4825e37f29b384.png

Posted

The key is to make the Hatch entity itself see-through not the boundary lines that get drawn. The hatch must also be checked after each boundary loop is added.

The main fix is the transparency value. For 50% see‑through use an alpha value 127 instead of 0.5 because Db.Transparency uses a 0–255 scale. Set it directly on the hatch for example: hat.setTransparency(Db.Transparency(127)). Also if traceBoundary returns loops find the outer loop and any inner islands separately before you call appendLoop. If you do not do this the hatch might fill spots that should stay open.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...