Author Topic: Redefine Block with VBA..  (Read 12418 times)

0 Members and 1 Guest are viewing this topic.

hardwired

  • Guest
Re: Redefine Block with VBA..
« Reply #15 on: April 08, 2008, 12:20:23 PM »
Hey Keith,

Me again, lol..

I've added you code to mine and when i run the program, which first checks if the block exists, which works, then i edit the userform as i'm meant to, then hit go and it errors out on the For Each OldBlkRef In ThisDrawing.PaperSpace line, with a Type Mismatch error even though there are blatantly other blocks (of the same name) in paperspace - would could be wrong?


Code: [Select]
'If Insert Chart (and update existing ones) is chosen..
ElseIf want1OPT.Caption = " Insert Chart (and update existing ones).." Then
[color=red]For Each OldBlkRef In ThisDrawing.PaperSpace[/color]
    With OldBlkRef
        If .Name = "Fixings_Chart" Then
            Set NewBlkRef = ThisDrawing.PaperSpace.InsertBlock(.InsertionPoint, .Name, .XScaleFactor, .YScaleFactor, .ZScaleFactor, .Rotation)
            NewBlkRef.Layer = .Layer
            NewBlkRef.color = .color
            NewBlkRef.Linetype = .Linetype
            NewBlkRef.LinetypeScale = .LinetypeScale
            NewBlkRef.Lineweight = .Lineweight
            NewBlkRef.Normal = .Normal
            NewBlkRef.Visible = .Visible
            .Delete
        End If
    End With
Next OldBlkRef
' Check if table exists and redefine (clearout) the block of all data before creating and inserting the new instance..
Dim Blk1 As AcadBlock
If acBlock.BlockExists("Fixings_Chart").Exists = True Then
  Set Blk1 = ThisDrawing.Blocks.Item("Fixings_Chart")
  CleanoutBlock Blk1 '<-- call the CleanoutBlock function here
End If
    CREATEChart
    INSERTChart


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Re: Redefine Block with VBA..
« Reply #16 on: April 08, 2008, 03:46:24 PM »
The problem is that there are more than block references in the drawing paperspace. You have to filter the object types to make it work.

Code: [Select]
    Dim OldBlkRef As AcadEntity ' AcadBlockReference is implied, but we cannot specify if other types are present in the collection
    Dim NewBlkRef As AcadBlockReference
   
    For Each OldBlkRef In ThisDrawing.PaperSpace
        With OldBlkRef
            ' filter for block references only
            If OldBlkRef.ObjectName = "AcDbBlockReference" Then
                If UCase(.Name) = "FIXINGS_CHART" Then
                    Set NewBlkRef = ThisDrawing.PaperSpace.InsertBlock(.InsertionPoint, .Name, .XScaleFactor, .YScaleFactor, .ZScaleFactor, .Rotation)
                    NewBlkRef.Layer = .Layer
                    NewBlkRef.color = .color
                    NewBlkRef.Linetype = .Linetype
                    NewBlkRef.LinetypeScale = .LinetypeScale
                    NewBlkRef.Lineweight = .Lineweight
                    NewBlkRef.Normal = .Normal
                    NewBlkRef.Visible = .Visible
                    ' transfer all of the attribute tag values over in this area
                    ' if you want to keep the existing values in the attributes
                    .Delete
                End If
            End If
        End With
    Next OldBlkRef
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

hardwired

  • Guest
Re: Redefine Block with VBA..
« Reply #17 on: April 09, 2008, 10:26:17 AM »
Hi Keith,

Done what you suggested and it doesn't error now on that bit, doesn't error at all. What it doesn't do either is update the blocks and attribute values, well not first time anyway. If i hit the Update Chart button to run the program once, it updates the blocks but not the attribute values, and if i hit the button again it seems to do the correct job and update the attributes aswell. Why would this be? And how can i get it to do it on first hit of the button?

Code snippet is below:

Code: [Select]
For Each OldBlkRef In ThisDrawing.PaperSpace
With OldBlkRef
    If OldBlkRef.ObjectName = "AcDbBlockReference" Then
        If .Name = "Fixings_Chart" Then
            Set NewBlkRef = ThisDrawing.PaperSpace.InsertBlock(.InsertionPoint, .Name, .XScaleFactor, .YScaleFactor, .ZScaleFactor, .Rotation)
            NewBlkRef.Layer = .Layer
            NewBlkRef.color = .color
            NewBlkRef.Linetype = .Linetype
            NewBlkRef.LinetypeScale = .LinetypeScale
            NewBlkRef.Lineweight = .Lineweight
            NewBlkRef.Normal = .Normal
            NewBlkRef.Visible = .Visible
            '.Delete
            ' Check if table exists and redefine (clearout) the block of all data before creating and inserting the new instance..
            If acBlock.BlockExists("Fixings_Chart").Exists = True Then
                Set Blk1 = ThisDrawing.Blocks.Item("Fixings_Chart")
                CleanoutBlock Blk1 '<-- call the CleanoutBlock function here
            End If
        End If 'If block is Fixings_Chart..
    End If 'If block..
End With
    CREATEChart
Next OldBlkRef


....the CleanoutBlock was the function you posted:

Code: [Select]
Function CleanoutBlock(currentBlock As AcadBlock)
    Dim blkItem As AcadEntity
    Dim blkAtt As AcadAttribute
    For Each blkItem In currentBlock
        blkItem.Delete
    Next blkItem
    For Each blkAtt In currentBlock
        blkAtt.Delete
    Next blkAtt
End Function

.....and the CREATEChart is my sub for creating the block and setting the attribute values..


Any ideas?

Bryco

  • Water Moccasin
  • Posts: 1850
Re: Redefine Block with VBA..
« Reply #18 on: April 09, 2008, 10:37:33 AM »
1) Update the block
2) Update the blockrefs

in that order

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Re: Redefine Block with VBA..
« Reply #19 on: April 09, 2008, 10:44:42 AM »
1) Update the block
2) Update the blockrefs

in that order
Indeed
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

hardwired

  • Guest
Re: Redefine Block with VBA..
« Reply #20 on: April 09, 2008, 12:07:15 PM »
Ok, tried that, won't work either, must be something in my code i am doing wrong. Could someone please take a look at moy code and run the program and let me know what might be wrong, would appreciate it muchly..

Oh, forgot to say, run it once to create and insert a chart to begin with, then run the program again and it should catch that there is already a chart(s) inserted..

hardwired

  • Guest
Re: Redefine Block with VBA..
« Reply #21 on: April 09, 2008, 12:22:15 PM »
Ok, sorry, changed the code about a bit more and it works perfectly..

Only thing now is if there NO INSERTED references BUT one in the collection, when you run the program it merges the two (the existing in the collection and the new updated one). Is there a way to purge out just that block (only if noreferences are INSERTED) before the program runs that part of the program?

When i purge out that block ref manually, and re-run the program it works, it just doesn't like any refs in the collection..

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Re: Redefine Block with VBA..
« Reply #22 on: April 09, 2008, 01:32:12 PM »
When I get some more time I'll look at it
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

hardwired

  • Guest
Re: Redefine Block with VBA..
« Reply #23 on: April 10, 2008, 08:10:54 AM »
Right, tested it (using the same code as the one i upload on my last post) and came up with the following problems:

  • When first run, and the chart is inserted for the first time (NO inserted refs or ones in the blocks collection), everything runs perfectly, except that it insertes to 0,0,0 and not the coords in the X and Y textboxes..
  • When first run, and the chart is inserted and there are NO other inserted BUT the block is in the block collection (ie: ran the program once, created the chart, deleted the inserted chart, ran the program again), it merged the block in the collection with the newly inserted one and its all a mess..
  • When run with charts already inserted (the program catches this and loads the attribute info to the form), and the UPDATE EXISTING CHARTS ONLY option is chosen, the charts update as they should visually, but i noticed that instead of deleting the existing attributes, or updating them, it adds more (each time you run it), so it has multiple instances of the same attibute, so instead of maximum of 10, it has 20 or so, which can be seen if you check the properties of the chart..
  • When run with charts already inserted and the INSERT CHART (AND UPDATE CHARTS ONES) option is chosen, the chart updates (but with same multiple attributes problem) but also the new chart is not inserted..

Sorry to be a PITA about all this Keith, but i can't get my head around it all and wanna get this one sorted..

Thanks for the help and for being patient :)

hardwired

  • Guest
Re: Redefine Block with VBA..
« Reply #24 on: April 10, 2008, 10:02:49 AM »
Ahhh, right on more tests (damn this thing, its really working my nips now!!)..

  • If there are more than one charts on a layout, the child (copied) blocks have the multiple attribute problem but the main parent one doesn't and it seems to do multiply by however many instances are inserted on that layout. The second block will have twice as many attributes (copies of the originals) and the third will have 3 times as many, and so on....
  • Also, it doesn't update other inserted references on OTHER layouts - well it does do the geometry, but not the attributes - so if there are any on another layout, then these will graphically (line etc) look right but they might have the attributes going out of the bottom of the chart or too few with empty spaces in the chart..

I would just do this with text in the block not attributes and be doen with it, but i won't be able to access the text and populate the form if the chart already exists - or will i? Can i use text instead of attributes and still have the same functionality of the program? Grrrrrrr

hardwired

  • Guest
Re: Redefine Block with VBA..
« Reply #25 on: May 29, 2008, 06:24:08 AM »
Hi,

I know this is an old one now, but can anyone take a look at this when they have time and point me home. Its 99ish% done and seems silly not to finish it..

Check out the last few threads for what the program is and isn't doing right and download the last posted dvb file..

Thanks in advance to anybody who cares, lol