Good moring,
yesterday I ran following code:
Sub AddReferences() Dim p3dMasterPoint As Point3d Dim p3dReferencePoint As Point3d Dim atcReference As Attachment Dim strFileSpec As String Dim i As Integer Dim j As Long Dim k As Long Const constMoveAlongX = 4700 Const constMoveAlongY = 3386 k = 1 strFileSpec = Application.ActiveDesignFile.FullName For i = 1 To 50 For j = 1 To 10 p3dMasterPoint.X = constMoveAlongX * (j - 1) p3dMasterPoint.Y = constMoveAlongY * (k - 1) p3dMasterPoint.Z = 0# p3dReferencePoint = Point3dZero Set atcReference = ActiveModelReference.Attachments.Add(strFileSpec, ".Template", CStr((i - 1) * 10 + j), "Sketch n° " & CStr((i - 1) * 10 + j), p3dMasterPoint, p3dReferencePoint) atcReference.NestLevel = 1 Next j k = k - 1 j = 0 Next i End Sub
and it worked well.
This morning I opened again the file but I didn' find any attachments on "Default" model. So I opened References Dialog window and I Found all references. But in the Attachment Settings window I saw Nested Attachments as "No Nesting". Please, see the attached image.
Why? How can I solve it with code? I wish to avoid setting as "Live Nesting" manually for 500 references.
A second question.
When i launched the code, it blocked microstation ... as if it were looping. So I used debug mode, running only one j loop at a time without problems. I just noticed, going forward, each attachement was slower and slower. Could you kindly tell me the reason and possibly suggest the solution?
Thanks a lot,
Paul
Hi Paolo,
please, never ask more questions in one post! Especially when they are about very different topics (nesting and performance in this case).
Paolo Maggiani said:Why?
I do not see any rewrite when NestingLevel is set.
Paolo Maggiani said:When i launched the code, it blocked microstation ... as if it were looping.
I guess it's not any surprise ;-)
Paolo Maggiani said:Could you kindly tell me the reason and possibly suggest the solution?
Without testing case, it's hard to guess. Doing 500 iterations of design file content has many consequences, especially when VBA is used (VBA is like external process, interacting through COM API).
Always, when more iterations are done, you should call DoEvents regularly to allow OS and host application to do their home works and maintenance tasks. It's not directly related to performance, but overall communication between code and host application.
I also recommend to stop using (I guess I mentioned it already earlier) the way to declare everything at the beginning. In my opinion it's very bad practice, leading to dirty and unstructured code and consequently to more errors. Declared variable when it's needed, never earlier.
In addition to better readability, it helps interpreter (and compiler in other languages) to better analyze / distinguish when the variable (and allocated memory) is used and when it can be cleared.
Sub AddReferences() Const constMoveAlongX = 4700 Const constMoveAlongY = 3386 Dim k As Long k = 1 Dim strFileSpec As String strFileSpec = Application.ActiveDesignFile.FullName Dim i As Integer For i = 1 To 50 Dim j As Long For j = 1 To 10 Dim p3dMasterPoint As Point3d p3dMasterPoint.X = constMoveAlongX * (j - 1) p3dMasterPoint.Y = constMoveAlongY * (k - 1) p3dMasterPoint.Z = 0# Dim atcReference As Attachment Set atcReference = ActiveModelReference.Attachments.Add(strFileSpec, ".Template", CStr((i - 1) * 10 + j), "Sketch n° " & CStr((i - 1) * 10 + j), p3dMasterPoint, Point3dZero) atcReference.NestLevel = 1 ' I think this is missing in your code atcReference.Rewrite Next j k = k - 1 j = 0 Next i End Sub
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Jan Šlegr said:please, never ask more questions in one post! Especially when they are about very different topics
I'll do.
Jan Šlegr said:Always, when more iterations are done, you should call DoEvents regularly
I'll do and I did in this case: it works better. :-)
Jan Šlegr said:I also recommend to stop using (I guess I mentioned it already earlier) the way to declare everything at the beginning.
I'll do (but it couldn't be easy)
Thanks a lot.
P
Paolo Maggiani said:I'll do (but it couldn't be easy)
It's much easier than you think! :-)
For all new code (the old one can be kept untouched) use Dim just before it's used. When this rule is applied with another crucial rule - to write very short methods, every with single responsibility (up to 10 lines) - one from consequences is that it's necessary to use only a few variables in every subroutine or function.