block attribute text alignment

block attribute text alignment

joshua_haleyFS9U6
Explorer Explorer
2,237 Views
9 Replies
Message 1 of 10

block attribute text alignment

joshua_haleyFS9U6
Explorer
Explorer

I realize my code is in python using pyautocad, but maybe the fundamental problem could be solved here, and I could implement it within my code.  I am creating a Bill of Materials table from an excel sheet.  I have created blocks with attributes referring to the column names within the table.  Whenever I set the ".Alignment" property (line 65) to align "Description" within its area, it seems to become aligned according to the insertion point of the block i.e. (0, 0, 0) currently I have hard coded the aDouble within the ".AddAttribute" method to center each attribute text.  I apologize in advance if this is in the wrong group. 

0 Likes
Accepted solutions (1)
2,238 Views
9 Replies
Replies (9)
Message 2 of 10

Ed__Jobe
Mentor
Mentor

Please don't post images of code. Its too hard to read and impossible to test without retyping. Please use a code window  using the </> button.

 

According to Help, the Left alignment uses the InsertionPoint of the attribute/text. So all alignment options are relative to the text, not the container. Unless there is a bug in pyautocad.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 3 of 10

daniel_cadext
Advisor
Advisor
Maybe use acAttributeModeLockPosition = 16
https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-53417B1F-C6F1-4818-AD43-8C5F81E36BCA

I’m wondering why you’re not using a table?
btw If you’re coding in Python, you might want to have a look here
https://github.com/CEXT-Dan/PyRx
and
https://www.theswamp.org/index.php?board=76.0
Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
Message 4 of 10

joshua_haleyFS9U6
Explorer
Explorer

I'm building a bill of materials table from an excel sheet.  Right now, I'm hard coding the insertion points (Line 28-32) for the attribute text in order to center them within their respective spaces. 

 

from pyautocad import ACAD, Autocad, APoint, aDouble

acad = Autocad(create_if_not_exists=True)

ip = APoint(0, 0, 0)

yStack = 0.0

blockName = "BOM Header"
b1 = acad.doc.Blocks.Add(ip, blockName)
p1 = b1.AddPolyline(aDouble(0,0,0, 5.3382,0,0, 5.3382,.1250,0, 0,.1250,0, 0,0,0))
a1 = b1.AddAttribute(.0625, 0, "BOM", aDouble(2.3, .03125, 0), "BOM", "BILL OF MATERIAL")

blockRef = acad.model.InsertBlock(APoint(10, 10 + yStack, 0), blockName, 1, 1, 1, 0)

yStack = -.1250
row = 2

blockName = "BOM Header 2"
b1 = acad.doc.Blocks.Add(ip, blockName)
p1 = b1.AddPolyline(aDouble(0,0,0, 5.3382,0,0, 5.3382,.1250,0, 0,.1250,0, 0,0,0))

l1 = b1.AddLine(APoint(.3139, 0, 0), APoint(.3139, .1250, 0))
l2 = b1.AddLine(APoint(.5970, 0, 0), APoint(.5970, .1250, 0))
l3 = b1.AddLine(APoint(2.9096, 0, 0), APoint(2.9096, .1250, 0))
l4 = b1.AddLine(APoint(4.3024, 0, 0), APoint(4.3024, .1250, 0))

a1 = b1.AddAttribute(.0625, 0, "ITEM", aDouble(.05, .03125, 0), "ITEM", "ITEM")
a2 = b1.AddAttribute(.0625, 0, "QTY", aDouble(.3639, .03125, 0), "QTY", "QTY")
a3 = b1.AddAttribute(.0625, 0, "DESCRIPTION", aDouble(1.45, .03125, 0), "DESCRIPTION", "DESCRIPTION")
a4 = b1.AddAttribute(.0625, 0, "PART", aDouble(3.3, .03125, 0), "PART", "PART NUMBER")
a5 = b1.AddAttribute(.0625, 0, "MANUFACTURER", aDouble(4.45, .03125, 0), "MANUFACTURER", "MANUFACTURER")

blockRef = acad.model.InsertBlock(APoint(10, 10 + yStack, 0), blockName, 1, 1, 1, 0)

 

joshua_haleyFS9U6_0-1697642129109.png

 

I was wondering if there was a way to set each columns text to a middle alignment so it could center any text dynamically and not reference the fixed insertion point. I've tried to use "a3.Alignment = 1" in reference the acAlignment enum values but it seems to align it to the block insertion point.

 

joshua_haleyFS9U6_1-1697643284639.png

 

0 Likes
Message 5 of 10

joshua_haleyFS9U6
Explorer
Explorer

I have been told these need to be attributed blocks.  It does seem a table would be more appropriate. 

0 Likes
Message 6 of 10

norman.yuan
Mentor
Mentor

This may not suit your business process, but here it goes anyway.

 

If the data source is an Excel sheet, I'd always try create a AutoCAD Table (only table itself: title row, columns) first, then use DataLink to link the table from the sheet. Easy and fast. Any data change in the sheet would be updated to the table automatically. No programming is required (and yes, code with .NET API can be used to manipulate Datalink at runtime, if needed).

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 10

daniel_cadext
Advisor
Advisor
Accepted solution

It seems you need to set the TextAlignmentPoint

Not sure if this is the behavior you want, but try

 

      a3.Alignment = Ax.constants.acAlignmentCenter
      a3.TextAlignmentPoint = (1.45, .03125,0)
Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
Message 8 of 10

daniel_cadext
Advisor
Advisor

Also, if you wanted to try a Table,  openpyxl is amazing,

note I’m using PyRx, but this sample is activex, so it should be easy enough to recreate with pyautocad

 

import PyRx as Rx
import traceback

#ActiveX
import AxApp24 as Ax
import openpyxl as Ex

def PyRxCmd_doit():
    try:
        axApp = Ax.getApp()
        axDoc = axApp.ActiveDocument
        
        wb = Ex.load_workbook('E:\\ItsAlive.xlsx')
        ws = wb.active

        pos = axDoc.Utility.GetPoint("\nPick a point: ")

        axTable = axDoc.ModelSpace.AddTable(
            pos, ws.max_row + 2, ws.max_column, 10, 40)
        
        for nrow in range(2,ws.max_row + 2):
            for ncol in range(ws.max_column):
                axTable.SetTextString(
                    nrow,ncol,1,ws.cell(
                        row=nrow, column=ncol+1).value)
                
    except Exception as err:
        traceback.print_exception(err)

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
Message 9 of 10

joshua_haleyFS9U6
Explorer
Explorer

For anyone referencing this post, I solved my Issue. In order to change the Text Alignment Point you must first change the attribute reference Alignment Point.  You cannot modify the Text Alignment Point until you do this.  To set the attribute text alignment point to middle center do this - 

(python using pyautocad)

attributeReference.Alignment = 10

attributeReference.TextAlignmentPoint = aDouble(x, y, z coordinates within block to align text)

0 Likes
Message 10 of 10

joshua_haleyFS9U6
Explorer
Explorer

Saw this solution after I posted mine lol.  I must have overlooked the remarks in the docs about having to change the alignment from left or aligned to anything else in order to be able to set the Text Alignment Point. Thank you for your time!