VBA to add automated centerlines

VBA to add automated centerlines

sam
Advocate Advocate
1,300 Views
10 Replies
Message 1 of 11

VBA to add automated centerlines

sam
Advocate
Advocate

Hi All, 

I have following code which places a base view and projected view of a part. Next I am trying to place the automated hole center lines but they are not showing up. Strangely when I debug the code to investigate what is wrong, the center points are correctly created as intended. Any idea what I am doing wrong?

Sub btnGenerateDrawing_Click()
'------ Locating the Template ------'
    strLocation = ThisApplication.FileOptions.TemplatesPath
    Set oDrgDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, strLocation & "Test.idw")
    Dim oSheet As Sheet
    Set oSheet = oDrgDoc.Sheets.Item(1)
       
    Dim oPoint1 As Point2d
    Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 25#)
    
    Dim oPoint2 As Point2d
    Set oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 15#)
    
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.Documents.Open("D:\Updated\Drawing Example\TestPart.ipt", False)

    Dim oView1 As DrawingView
    Set oView1 = oSheet.DrawingViews.AddBaseView(oPartDoc, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)

    Dim oView2 As DrawingView
    Set oView2 = oSheet.DrawingViews.AddProjectedView(oView1, oPoint2, kFromBaseDrawingViewStyle, 0.1)
    
    Dim settings As AutomatedCenterlineSettings
    Set settings = oDrgDoc.DrawingSettings.AutomatedCenterlineSettings
    
    
    settings.ApplyToHoles = True
    settings.ProjectionParallelAxis = True
    Call oView1.SetAutomatedCenterlineSettings(settings)
    Call oView2.SetAutomatedCenterlineSettings(settings)

    Call oPartDoc.Close(True)
    
End Sub

 

best regards, 

sam

0 Likes
Accepted solutions (1)
1,301 Views
10 Replies
Replies (10)
Message 2 of 11

JhoelForshav
Mentor
Mentor

Hi @sam 

I did some testing and it seems to work fine if you set the AutomatedCenterlineSettings-object by GetAutomatedCenterlineSettings.

Try this code and see if it works 🙂

 

Sub btnGenerateDrawing_Click()
'------ Locating the Template ------'
    strLocation = ThisApplication.FileOptions.TemplatesPath
    Set oDrgDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, strLocation & "Test.idw")
    Dim oSheet As Sheet
    Set oSheet = oDrgDoc.Sheets.Item(1)
       
    Dim oPoint1 As Point2d
    Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 25#)
    
    Dim oPoint2 As Point2d
    Set oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 15#)
    
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.Documents.Open("D:\Updated\Drawing Example\TestPart.ipt", False)

    Dim oView1 As DrawingView
    Set oView1 = oSheet.DrawingViews.AddBaseView(oPartDoc, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)

    Dim oView2 As DrawingView
    Set oView2 = oSheet.DrawingViews.AddProjectedView(oView1, oPoint2, kFromBaseDrawingViewStyle, 0.1)
    
    Dim settings As AutomatedCenterlineSettings
    Call oView1.GetAutomatedCenterlineSettings(settings)
    
    
    settings.ApplyToHoles = True
    settings.ProjectionParallelAxis = True
    Call oView1.SetAutomatedCenterlineSettings(settings)
    Call oView2.SetAutomatedCenterlineSettings(settings)

    Call oPartDoc.Close(True)
    
End Sub
Message 3 of 11

sam
Advocate
Advocate

Hi @JhoelForshav

Thanks for the reply, I updated as your advice and result is still same. Also if I debug the code, centerlines are generated as was case in my original question code. 

Sub btnGenerateDrawing_Click()
    '------ Locating the Template ------'
    strLocation = ThisApplication.FileOptions.TemplatesPath
    Set oDrgDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, strLocation & "TestNew.idw")
    Dim oSheet As Sheet
    Set oSheet = oDrgDoc.Sheets.Item(1)

    Dim oPoint1 As Point2d
    Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 25#)

    Dim oPoint2 As Point2d
    Set oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 15#)

    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.Documents.Open("D:\Updated\Drawing Example\Test.ipt", False)
    

    Dim oView1 As DrawingView
    Set oView1 = oSheet.DrawingViews.AddBaseView(oPartDoc, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)

    Dim oView2 As DrawingView
    Set oView2 = oSheet.DrawingViews.AddProjectedView(oView1, oPoint2, kFromBaseDrawingViewStyle, 0.1)

    Dim oPoint3 As Point2d
    Set oPoint3 = ThisApplication.TransientGeometry.CreatePoint2d(-70#, -20#)
    Dim oPoint4 As Point2d
    Set oPoint4 = ThisApplication.TransientGeometry.CreatePoint2d(-70#, 20#)

    Dim oDrawingSketch As DrawingSketch
    Set oDrawingSketch = oView1.Sketches.Add

    oDrawingSketch.Edit
    Dim oSketchLine As SketchLine
    Set oSketchLine = oDrawingSketch.SketchLines.AddByTwoPoints(oPoint3, oPoint4)

    oDrawingSketch.ExitEdit

    Dim oPoint5 As Point2d
    Set oPoint5 = ThisApplication.TransientGeometry.CreatePoint2d(35#, 0#)

    Dim oView3 As SectionDrawingView

    Set oView3 = oSheet.DrawingViews.AddSectionView(oView1, oDrawingSketch, oPoint5, kHiddenLineDrawingViewStyle, 0.2, True, "A")
    oView3.DisplayHatching = False
    
    Dim settings As AutomatedCenterlineSettings
    Call oView1.GetAutomatedCenterlineSettings(settings)
    Call oView2.GetAutomatedCenterlineSettings(settings)
    Call oView3.GetAutomatedCenterlineSettings(settings)
  
    settings.ApplyToHoles = True
    settings.ApplyToCylinders = False
    settings.ApplyToRevolutions = False
    settings.ProjectionParallelAxis = True

    Call oView1.SetAutomatedCenterlineSettings(settings)
    Call oView2.SetAutomatedCenterlineSettings(settings)
    Call oView3.SetAutomatedCenterlineSettings(settings)
    Call oPartDoc.Close(True)
End Sub

 

Can you please have another look at it that why it is happening? Could it be something to do with my any other settings? 

I am using Inv2019.4.5. 

Regards, 

Sam

0 Likes
Message 4 of 11

JhoelForshav
Mentor
Mentor

@sam 

I can see that my suggestion wouldn't really make a difference. Woth trying it out though...

I assume that the holes are actual hole features? If they're extrusions for example you'll need to set settings.ApplyToCylinders = True.

Message 5 of 11

sam
Advocate
Advocate

Hi @JhoelForshav , 

 

They are actually hole features. 

sam_0-1590487345367.png

1. If I right click and chose setting as in our code above centerlines are created. 

2. If I debug my original code or the code advised by you again the centerlines are created. 

 

It is only when I run and drawing is generated they don't show up. 

 

regards, 

sam

0 Likes
Message 6 of 11

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @sam 

Now I understand the problem!

I did some testing now and it seems the code is simply moving to fast for itself. When i added a little pause in it it worked 🙂

 

Try this code:

Sub btnGenerateDrawing_Click()
    '------ Locating the Template ------'
    strLocation = ThisApplication.FileOptions.TemplatesPath
    Set oDrgDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, strLocation & "TestNew.idw")
    Dim oSheet As Sheet
    Set oSheet = oDrgDoc.Sheets.Item(1)

    Dim oPoint1 As Point2d
    Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 25#)

    Dim oPoint2 As Point2d
    Set oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 15#)

    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.Documents.Open("D:\Updated\Drawing Example\Test.ipt", False)
    

    Dim oView1 As DrawingView
    Set oView1 = oSheet.DrawingViews.AddBaseView(oPartDoc, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)

    Dim oView2 As DrawingView
    Set oView2 = oSheet.DrawingViews.AddProjectedView(oView1, oPoint2, kFromBaseDrawingViewStyle, 0.1)

    Dim oPoint3 As Point2d
    Set oPoint3 = ThisApplication.TransientGeometry.CreatePoint2d(-70#, -20#)
    Dim oPoint4 As Point2d
    Set oPoint4 = ThisApplication.TransientGeometry.CreatePoint2d(-70#, 20#)

    Dim oDrawingSketch As DrawingSketch
    Set oDrawingSketch = oView1.Sketches.Add

    oDrawingSketch.Edit
    Dim oSketchLine As SketchLine
    Set oSketchLine = oDrawingSketch.SketchLines.AddByTwoPoints(oPoint3, oPoint4)

    oDrawingSketch.ExitEdit

    Dim oPoint5 As Point2d
    Set oPoint5 = ThisApplication.TransientGeometry.CreatePoint2d(35#, 0#)

    Dim oView3 As SectionDrawingView

    Set oView3 = oSheet.DrawingViews.AddSectionView(oView1, oDrawingSketch, oPoint5, kHiddenLineDrawingViewStyle, 0.2, True, "A")
    oView3.DisplayHatching = False
    
    Dim settings As AutomatedCenterlineSettings
    Call oView1.GetAutomatedCenterlineSettings(settings)
  
    settings.ApplyToHoles = True
    settings.ApplyToCylinders = False
    settings.ApplyToRevolutions = False
    settings.ProjectionParallelAxis = True

    Dim Start As Long
    Dim I As Long
    I = 2 'If it doesn't work, try a higher value for I (wait time)

    Start = Timer ' Set start time.
    Do While Timer < Start + I
    DoEvents ' Yield to other processes.
    Loop
    

    Call oView1.SetAutomatedCenterlineSettings(settings)
    Call oView2.SetAutomatedCenterlineSettings(settings)
    Call oView3.SetAutomatedCenterlineSettings(settings)
    Call oPartDoc.Close(True)
End Sub
Message 7 of 11

sam
Advocate
Advocate

@JhoelForshav Wow it did fix the problem.  Also I have changed the pause to .5 as pause of 2 was too dramatic 🙂. It is fascinating to see this has solved the issue but on other hand it does make things complicate for a beginner like myself, like how would I know that this could have solved the problem or code is too fast (I am not complaining, just thinking loud).. I guess the answer is more and more practice and keep looking around other solutions of different problems. 

 

Thanks a lot, 

 

regards, 

sam 

Message 8 of 11

JhoelForshav
Mentor
Mentor

@sam 

It was really just a fluke that I found it. While debugging i added a messagebox to report back some values. The pause caused by the messagebox made it work so I just googled a way to create a pause in VBA (I usually only write code in VB.Net/iLogic).

 

I'm glad I was able to help 🙂

Message 9 of 11

sam
Advocate
Advocate

Hi @JhoelForshav

As I am progressing and learning bit more - I am realizing that VB.NET is way better option. 

I am making case to get VS installed and hoping to get it soon. 

 

Best regards, 

Sam

Message 10 of 11

sam
Advocate
Advocate

@JhoelForshav Found a bit shorter code to turn on centerlines. Yet this only works either during debugging or only with help of your delay code..

 

Dim oViews As DrawingViews
Dim oNumView As DrawingView
Dim ViewCount As Integer
Dim StartCount As Integer
Dim oview As DrawingView

StartCount = 0

'number of drawing views
ViewCount = ThisApplication.ActiveDocument.ActiveSheet.DrawingViews.Count

'define collection of views objects
Set oViews = ThisApplication.ActiveDocument.ActiveSheet.DrawingViews

' Places Centerlines on all of the drawing views
For StartCount = 1 To ViewCount
Call oViews.Item(StartCount).SetAutomatedCenterlineSettings
Next

Message 11 of 11

sam
Advocate
Advocate

@JhoelForshav hi I have found a way (formula to use in every dimension) which is working perfectly as of now. Thanks for guiding me with your previous work. this is how I am keeping the dim at middle. 

 

Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d((oCenterMark1.Position.X + (oCenterMark2.Position.X - oCenterMark1.Position.X) / 2), 22)

 

 

best regards, 

Sam

 

0 Likes