Recent Posts

Pages: [1] 2 3 ... 10
1
.NET / Re: What is the difference between a Block and Blockreference
« Last post by nullptr on March 25, 2017, 09:37:18 pm »
Also, Block References can 'own' Attribute entities
2
.NET / Re: inserting dynamic block by name, or reading dyn props (with example)
« Last post by nekitip on March 25, 2017, 08:49:35 pm »
...i have a strange feeling that dynprop collection should also be created and disposed on non-temporary object in the same way as in temp object. I've had strange crashes, and I came to suspect that, yet I have not found any source online to confirm this observation...
Other sources online do not mention explicit disposal like here
http://through-the-interface.typepad.com/through_the_interface/2009/03/accessing-the-properties-of-a-dynamic-autocad-block-using-net.html
...however, I do see that they are not iterating directly, but rather also they first create collection (maybe that's all that is needed), however, that's kind of not intuitive. I'm not sure if I can confirm that, since crashes are not following any pattern and are rare, but once I've set .dispose to this location also, they are mostly gone...
I use ac2018 (but app is compiled with .net 4.0)
3
VB(A) / to make a DWG , from an XLS, by VBA , from the VBA editor at xls
« Last post by DEVITG on March 25, 2017, 02:07:39 pm »
I have a:
1st
xls with columns block-name att1-tag to attn-tag could be about 15 att. and columns ,at the same row with att-values. last colummn to set order number or empty , to insert the block. All data at the same row

2nd
a dwt template , modelspace empty
the block collection at Adoc
all features by user

3rd Populate the modelsapce with blocks in a row as wide has paper space wide , and a line conecting each block with subsecuent block .

Itīs kind of a unifilar circuit , no branchs

4th save as phat/project.dwg
Hope it is clear
5th print the dwg as pdf file
6th Open the pdf file
The user will never manipulate the dwg , it is a commercial person , no ACAD skill.
Hope it is clear


Or at least I need to see xls-VBA to dwg from XLS , NOT acad-VBA to dwg.
Iīm proficient in VLISP, I want to start in this new task.
Thank in advance

Its a project in my mind , not materialized neither in XLS and DWT

I just want to know if it is possible , and the first step on XLS VBA to ACAD .

Off course the user station hold XLS, itīs ACAD VBA complements , and ACAD,

Thanks in advance.

Quote
The longest journey, start with the first step, No matter how short the step is.
4
.NET / Re: inserting dynamic block by name, or reading dyn props (with example)
« Last post by nekitip on March 25, 2017, 12:58:32 pm »
Perfect answer!
Exactly what I needed/was missing/was not sure of.
-so for everyone who googles to here, please read entire anwser from n.yuan, it will help you alot.
(most important parts: when creating, set dynamic properties last (and remember that transformations go from top to bottom of hyararchy), and while reading block by name (and planing to JUST read), temporary create dynamic block reference, take dynprop collection, and dispose both when finished!).
5
.NET / Re: inserting dynamic block by name, or reading dyn props (with example)
« Last post by n.yuan on March 25, 2017, 09:18:49 am »
I can see a few issues.

About Inserting a dynamic block (BlockReference), your first CommandMethod:

1. You need to create AttributeReference in the BlockReference first BEFORE apply dynamic properties. That is, the BlockReference is first created as a NORAML BlockReference based on the BlockTableRecord (definition). When dynamic properties are applied to a BlockReference, depending on the nature of dynamic properties, their values..., AutoCAD may create an anonymous BlockTableRecord and the said BlockReference become a reference of the anonymous BlockTableRecord.

In your inserting code, you set dynamic property first, and then add AttributeReference, and you question how to make attribute in right position. While I do not know what the dynamic property does, but I guess it may have something to do with the attribute, thus your code does not place attribute in the place as you expected. So, change the code order to adding attribute first, then set dynamic property after.

2. Your code of setting dynamic property could be problematic: you set its value regardless if the dynamic property is the one expected or not. That is, your code only works if there is only one property, the code would be OK (you do set a variable dpToSearch="xxxx" but never used it. To make code safe, you should test property's name, and only set its value when the property is the one which expects certain value

3. One rarely needs to worry about Transaction.XXXXXGraphicsFlush(). I never needed use them in my AutoCAD .NET API practice, so far.

4. db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES") is used/needed if the block is annotative. That is, if a block is defined as annotative, the block reference should also be annotative

About your second CommandMethod

I am not very sure I know what you want to do here.

Yes, loop through a BlockTableRecord, you can obtain a list of attribute a NORMAL block reference should have. But one should know, attribute definition in block definition only serve as template for attribute reference to be created. When creating AttributeReference, the code has the freedom to actually create the AttributeReference or not. So, having a attribute list from block definition does not guarantee all block references have the same attributes.

As for getting a list of dynamic properties defined for particular block (BlockTableRecord), unfortunately, there is no API exposed to dynamic property definitions. They stored in drawing, somehow linked to the BlockTableRecord as undocumented NamedDictionary data and/or XData and/or ExtensionDictionary.  So, yes, you can only get a dynamic property list via an existing BlockReference by loop through its DynamicBlockReferencePropertyCollection
6
.NET / Re: inserting dynamic block by name, or reading dyn props (with example)
« Last post by nekitip on March 25, 2017, 08:54:13 am »
after a lot of hair loss this morning in a trial and error attempts, it looks (but if someone would confirm) that the first problem of crashing cad may be solved (not tested enough) by explicitly using collection of dyn props, and then disposing it.

Code - vb.net: [Select]
  1. If blockDef.IsDynamicBlock Then
  2.  
  3.                        'create new temp ref since i do now know how to get all dynprops
  4.                        'here, something is wrong. could be that there are more anonymous block created and they are not deleted with dispose
  5.                        'how to fix this? Or, how to find dynprop withot creating new ref?
  6.                        'UPDATE: maybe using dpcollection like this, and later dispose it
  7.                        Dim tempDynBlockRef As New BlockReference(New Point3d, blockDef.ObjectId)
  8.                        Dim dpcollection As DynamicBlockReferencePropertyCollection = tempDynBlockRef.DynamicBlockReferencePropertyCollection
  9.  
  10.                        For Each dp As DynamicBlockReferenceProperty In dpcollection
  11.                            If Not dp.ReadOnly Then attList.Add(dp.PropertyName)
  12.  
  13.                        Next
  14.                        dpcollection.Dispose()
  15.                        tempDynBlockRef.Dispose()
  16.  
  17.                    End If
7
.NET / inserting dynamic block by name, or reading dyn props (with example)
« Last post by nekitip on March 25, 2017, 07:41:15 am »
When we talk about blocks, there are a lof of examples online, and here is an example of both reading and writing dynamic block with attributes (by name).
However, my problems are:
-how to read dyn props without crashing (I'm missing something, but I do not know what)
-how to make attributes position themself on right position when we change visuals of blockref from code
-what for are QueueForGraphicsFlush and other commented stuff

Code - vb.net: [Select]
  1. Imports System
  2. Imports Autodesk.AutoCAD.Runtime
  3. Imports Autodesk.AutoCAD.ApplicationServices
  4. Imports Autodesk.AutoCAD.DatabaseServices
  5. Imports Autodesk.AutoCAD.Geometry
  6. Imports Autodesk.AutoCAD.EditorInput
  7.  
  8. <Assembly: CommandClass(GetType(test1.MyCommands))>
  9.  
  10. Namespace test1
  11.  
  12.  
  13.    Public Class MyCommands
  14.  
  15. #Region "commands"
  16.  
  17.        <CommandMethod("INSERTDYNAMICBLOCK")>
  18.        Public Shared Sub insertBlock()
  19.            Dim attList As New List(Of String)
  20.            Dim name As String = "RMNUM"
  21.  
  22.            Dim db As Database = HostApplicationServices.WorkingDatabase()
  23.            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(db)
  24.            Dim ed As Editor = doc.Editor
  25.  
  26.            Using trans As Transaction = db.TransactionManager.StartTransaction()
  27.  
  28.                ' what is this for?
  29.                'doc.TransactionManager.EnableGraphicsFlush(True)
  30.  
  31.                Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
  32.                Dim btr As BlockTableRecord = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
  33.  
  34.                'what is this for?
  35.                'Dim occ As ObjectContextCollection = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES")
  36.  
  37.                Dim blockDef As BlockTableRecord = trans.GetObject(bt(name), OpenMode.ForRead)
  38.                'if bt.has...then
  39.                Using acNewBlockRef As New BlockReference(New Point3d, blockDef.ObjectId)
  40.  
  41.                    btr.AppendEntity(acNewBlockRef)
  42.                    trans.AddNewlyCreatedDBObject(acNewBlockRef, True)
  43.  
  44.  
  45.                    'attributes
  46.                    'classic attrib must always have tag, but tag can be the same
  47.                    'dynamic prop name must be different
  48. 'HERE: BLOCK ATTRIBUTES ARE NOT TRANSFORMED WHEN CHANGING DP (or am I missing something simple somewhere)
  49.                    If acNewBlockRef.IsDynamicBlock Then
  50.                        For Each dp As DynamicBlockReferenceProperty In acNewBlockRef.DynamicBlockReferencePropertyCollection
  51.                            If Not dp.ReadOnly Then
  52.                                'now, IF we change dynprop (flip), then attrib is no longer at proper place without attsync
  53.                                Dim dpnameToSearch As String = "Flip state1"
  54.                                Dim newVal As Double = 1.0
  55.  
  56.                                Select Case dp.PropertyTypeCode
  57.                                    Case 1
  58.                                        dp.Value = newVal
  59.                                    Case 2
  60.                                        dp.Value = Convert.ToInt32(newVal Mod Int32.MaxValue)
  61.                                    Case 3
  62.                                        dp.Value = Convert.ToInt16(newVal Mod Int16.MaxValue)
  63.                                    Case 4
  64.                                        dp.Value = Convert.ToSByte(newVal Mod Byte.MaxValue)
  65.                                    Case 5
  66.                                        dp.Value = Convert.ToString(newVal)
  67.                                    Case 13
  68.                                        dp.Value = Convert.ToInt64(newVal Mod Int64.MaxValue)
  69.                                    Case Else
  70.                                End Select
  71.                            End If
  72.  
  73.                        Next
  74.                    End If
  75.  
  76.  
  77.  
  78.                    For Each attId As ObjectId In blockDef
  79.                        Dim ent As Entity = trans.GetObject(attId, OpenMode.ForRead)
  80.                        If TypeOf ent Is AttributeDefinition Then
  81.                            Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition)
  82.  
  83.                            If (attDef IsNot Nothing) AndAlso (Not attDef.Constant) Then
  84.                                'This is a non-constant AttributeDefinition
  85.                                Using attRef As New AttributeReference()
  86.  
  87.                                    attRef.SetAttributeFromBlock(attDef, acNewBlockRef.BlockTransform)
  88.                                    acNewBlockRef.AttributeCollection.AppendAttribute(attRef)
  89.                                    trans.AddNewlyCreatedDBObject(attRef, True)
  90.  
  91.                                    'CHECK HERE FOR THIS ATTRIB
  92.  
  93.                                    attRef.TextString = "test"
  94.                                    If attRef.HasFields Then
  95.                                        Dim fOif As ObjectId = attRef.GetField()
  96.                                        Dim fo As Field = trans.GetObject(fOif, OpenMode.ForWrite)
  97.                                        fo.Evaluate()
  98.                                    End If
  99.  
  100.                                End Using
  101.                            End If
  102.                        End If
  103.                    Next
  104.  
  105.                    'what is this for?
  106.                    'acNewBlockRef.RecordGraphicsModified(True) ' to force updating a block reference
  107.  
  108.                End Using
  109.  
  110.                'what is this for?
  111.                'trans.TransactionManager.QueueForGraphicsFlush()
  112.  
  113.                trans.Commit()
  114.            End Using
  115.        End Sub
  116.  
  117.        <CommandMethod("GETBLOCKATT")>
  118.        Public Shared Sub getBlockAttributesByBlockName(g)
  119.  
  120.            Dim name As String = "RMNUM"
  121.            Dim realName As String = ""
  122.            realName = name
  123.  
  124.            Dim attList As New List(Of String)
  125.  
  126.            Dim db As Database = HostApplicationServices.WorkingDatabase()
  127.            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(db)
  128.            Dim ed As Editor = doc.Editor
  129.  
  130.            Using trans As Transaction = db.TransactionManager.StartTransaction()
  131.  
  132.                Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
  133.                If bt.Has(name) Then
  134.  
  135.                    Dim blockDef As BlockTableRecord = trans.GetObject(bt(name), OpenMode.ForRead)
  136.                    realName = blockDef.Name
  137.                    If blockDef.HasAttributeDefinitions Then
  138.                        For Each chkObjID As ObjectId In blockDef
  139.                            Dim ent As Entity = trans.GetObject(chkObjID, OpenMode.ForRead)
  140.                            If TypeOf ent Is AttributeDefinition Then
  141.                                Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition)
  142.                                attList.Add(attDef.Tag)
  143.                            End If
  144.                        Next
  145.                    End If
  146.  
  147.                    If blockDef.IsDynamicBlock Then
  148. 'HERE: CRASH IN 50% TIMES
  149.                        'create new temp ref since i do now know how to get all dynprops
  150.                        'here, something is wrong. could be that there are more anonymous block created and they are not deleted with dispose
  151.                        'how to fix this? Or, how to find dynprop withot creating new ref?
  152.                        Dim tempDynBlockRef As New BlockReference(New Point3d, blockDef.ObjectId)
  153.  
  154.                        For Each dp As DynamicBlockReferenceProperty In tempDynBlockRef.DynamicBlockReferencePropertyCollection
  155.                            If Not dp.ReadOnly Then attList.Add(dp.PropertyName)
  156.                        Next
  157.  
  158.                        tempDynBlockRef.Dispose()
  159.  
  160.                    End If
  161.                End If
  162.                trans.Commit()
  163.            End Using
  164.  
  165.            'result is:
  166.            'realname with real name of block
  167.            'attlist with att tag and dynprop names
  168.  
  169.  
  170.        End Sub
  171.  
  172. #End Region
  173.  
  174.    End Class
  175. End Namespace
8
Plotting / Re: Publishing
« Last post by Refri on March 25, 2017, 03:40:26 am »
Pukenzz
We use this solution:
Revers - Automatic batch printing a plurality of frames (format) of the model space and layouts.
http://www.kdmsoft.ru/reverseng.html
9
AutoLISP (Vanilla / Visual) / Re: Routine wanted to modifile
« Last post by kottok on March 24, 2017, 11:55:22 pm »
Hi Peter,

i added a DCL , but the result not satisfied.
10
.NET / Re: Updating MTEXT C#
« Last post by nocent on March 24, 2017, 03:57:54 pm »
Hi Ken,

Method i was having trouble with (note i also had to add the "document lock" to solve the issue production):
Code: [Select]
public static void EditMtextContents(UpdatedTag tag)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;

//create handle from long int
Handle hn = new Handle(ConvertHandleToInt(tag.Handle));

//attempt to get an ObjectID for the handle
ObjectId id = db.GetObjectId(false, hn, 0);

//open the object to edit it
using (DocumentLock doclock = doc.LockDocument())
{
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
DBObject obj = tr.GetObject(id, OpenMode.ForWrite);
//testing editing mtext contents
MText mtext = obj as MText;
mtext.Contents = tag.NewValue;
tr.Commit();
}
}
}

Updated tag is just a small simple class:
Code: [Select]
public class UpdatedTag
{
public string Handle { get; set; }
public string NewValue { get; set; }

public UpdatedTag(string handle, string newValue)
{
Handle = handle;
NewValue = newValue;
}
}

I extract all the mtext and store it in a list/dataset.  part of that information is its "handle" in acad terms. 
i convert the string handle to an autocad object handle using (i believe i got this off from Kean, either way hes been a huge resource):
Code: [Select]
public class UpdatedTag
{
public string Handle { get; set; }
public string NewValue { get; set; }

public UpdatedTag(string handle, string newValue)
{
Handle = handle;
NewValue = newValue;
}
}


Hope this is more helpful than confusing - am very much new to acad and development in general :)
Pages: [1] 2 3 ... 10