Generating sections

Generating sections

davidhellberg
Contributor Contributor
1,278 Views
6 Replies
Message 1 of 7

Generating sections

davidhellberg
Contributor
Contributor

hi;

 

Has anyone used VBA to generate 2D sections from solids?

The AutoCAD reference for this part is not giving much clues.

 

I have been able to create some lines for the sections but when it comes to actually generate a block of the lines using the built-in methods I'm stuck.

 

The methods are 

 setSectionTypeSettings.GenerationOptions = acSectionGenerationDestinationNewBlock
vBlockGen = setSectionTypeSettings.DestinationBlock

 

The first line will generate an "Invalid Input" error which confuses me.

I guess I could actually just take the lines a create my own block but it would be nice if the built in method worked.

 

Anyone has some example code in this regards that they would be willing to share?

 

 

/David Hellberg

 

0 Likes
Accepted solutions (3)
1,279 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor
Accepted solution

I've never used vba to generate a section before (I don't use acad for 3D), but a search doesn't turn up any code samples. It looks like the vba api for sections is not very useful. That said, there is no setSectionTypeSettings method. Is that something you created? You may need to show all your code. Make sure to put in in a code window using the </> button.

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 7

davidhellberg
Contributor
Contributor

Hi Ed;

 

Thanks for your reply and as you say the internet is silent on this one.

I got my initial code from this post on AUGI

https://forums.augi.com/showthread.php?72798-Sectioning-Solids

and developed it and tried to figure it out further. But there are still many parts that I don't understand.

 

I had some development getting layers correct and which lines should be shown to some extent.

 

Here is the code. It is the end part that generates the error. 

Line 148:

"setSectionTypeSettings.GenerationOptions = acSectionGenerationDestinationNewBlock"

 

setSectionTypeSettings is actually "Dim setSectionTypeSettings As AcadSectionTypeSettings"

 

My solution right now is to collect all the generated objects and just create a block myself.

 

Code in Full:

Public Sub CreateSectionViewsFrom3DSolid()
                          
    Dim oAcadSec As AcadSection
    Dim SectionSettings As AcadSectionSettings
    Dim setSectionTypeSettings As AcadSectionTypeSettings
    Dim dPlaneVector(0 To 2) As Double
    
    Dim x3DSolid As Acad3DSolid
    Dim vBasePt As Variant
    Dim vPt1 As Variant
    Dim vPt2 As Variant

    ' Get the solid
    ThisDrawing.Utility.GetEntity x3DSolid, vBasePt, "Pick 3D Solid"
    
    ' get 2 section points
    vPt1 = ThisDrawing.Utility.GetPoint(, "Pick first point")
    vPt2 = ThisDrawing.Utility.GetPoint(, "Pick end point")
    
    
    ' The below vector [0,0,1] will give a vertical section through a solid
    dPlaneVector(0) = 0: dPlaneVector(1) = 0: dPlaneVector(2) = 1

    Err.Clear
    On Error Resume Next
'------------------------------------------------------------------------------
'   Insert Section object
'------------------------------------------------------------------------------
    
    ' Create the section object
    Set oAcadSec = ThisDrawing.ModelSpace.AddSection(vPt1, vPt2, dPlaneVector)

    With oAcadSec
        .Elevation = 350
        .State = acSectionStatePlane
        .State2 = acSectionState2Slice
        .SliceDepth = 250#
        Set SectionSettings = .Settings
        .Update
    End With
    
    With SectionSettings
        .CurrentSectionType = acSectionType2dSection
    End With

    Set setSectionTypeSettings = SectionSettings.GetSectionTypeSettings(acSectionType2dSection)
    
'------------------------------------------------------------------------------
'   Section object settings
'------------------------------------------------------------------------------
    
                ' Section Settings Background lines
                With setSectionTypeSettings
                    .BackgroundLinesVisible = False
                    .BackgroundLinesHiddenLine = False
                        .BackgroundLinesLayer = "PC Bea. Thn"
                        .BackgroundLinesLinetype = "ByLayer"
                        
                        '.BackgroundLinesColor=""
                        '.BackgroundLinesLinetypeScale=5
                        '.BackgroundLinesLineweight=0.18
                        '.BackgroundLinesPlotStyleName="UPH Monochrome.ctb"
                End With
             
                
                ' Section Settings InterSection boundary
                ' The outline of the Intersection Boundary
                With setSectionTypeSettings
                    .IntersectionBoundaryVisible = True
                        .IntersectionBoundaryLayer = "PC Pnl. Thn"
                        .IntersectionBoundaryLinetype = "ByLayer"
                        
                        '.IntersectionBoundaryColor=""
                        '.IntersectionBoundaryDivisionLines = False
                        '.IntersectionBoundaryLinetypeScale = 5
                        '.IntersectionBoundaryLineweight = 0.18
                        '.IntersectionBoundaryPlotStyleName = "UPH Monochrome.ctb"
                End With
                        
                ' Section Settings Intersection fill
                ' For a face when a section goes through the object
                With setSectionTypeSettings
                    .IntersectionFillVisible = False
                        '.IntersectionFillColor = "red"
                        '.IntersectionFillFaceTransparency = [long]             [Live Section Only]
                        '.IntersectionFillHatchAngle=45 [units radian??]
                        '.IntersectionFillHatchPatternName = "ANSI31"
                        '.IntersectionFillHatchPatternType = acHatchPatternTypePreDefined
                        '.IntersectionFillHatchScale = 5
                        '.IntersectionFillHatchSpacing = 150#
                        '.IntersectionFillLayer = "PC Htc"
                        '.IntersectionFillLinetype = "Continous"
                        '.IntersectionFillLinetypeScale = 5
                        '.IntersectionFillLineweight = 0.18
                        '.IntersectionFillPlotStyleName = "UPH Monochrome.ctb"
                End With
            
                ' Section Settings ForeGround Lines
                ' This is cut away geometry
                With setSectionTypeSettings
                    .ForegroundLinesVisible = False
                    .ForegroundLinesHiddenLine = False
                        '.ForegroundLinesColor=""
                        '.ForegroundLinesEdgeTransparency = [long]             [Live Section Only]
                        '.ForegroundLinesFaceTransparency= [long]             [Live Section Only]
                        '.ForegroundLinesLayer = "PC Pnl .Thn"
                        '.ForegroundLinesLinetype = "Continous"
                        '.ForegroundLinesLinetypeScale = 5
                        '.ForegroundLinesLineweight = 0.18
                        '.ForegroundLinesPlotStyleName = "UPH Monochrome.ctb"
                End With
            
                ' Section Settings CurveTangency Lines
                With setSectionTypeSettings
                    .CurveTangencyLinesVisible = False
                        '.CurveTangencyLinesColor=""
                        '.CurveTangencyLinesLayer="PC Pnl
                        '.CurveTangencyLinesLinetype="Continous"
                        '.CurveTangencyLinesLinetypeScale=5
                        '.CurveTangencyLinesLineweight = 0.18
                        '.CurveTangencyLinesPlotStyleName="UPH Monochrome.ctb"
                End With
            
                ' Section Settings Others
                With setSectionTypeSettings
                    '.GenerationOptions = acSectionGenerationDestinationNewBlock
                    '.DestinationBlock = "TestBlock"
                    '.DestinationFile = "C:\temp\file.dwg"
                    '.GenerationOptions = acSectionGenerationDestinationFile
                    
                    '.GenerationOptions = acSectionGenerationDestinationReplaceBlock
                    '.GenerationOptions = acSectionGenerationSourceAllObjects
                    '.GenerationOptions = acSectionGenerationSourceSelectedObjects
                    '.SourceObjects = vObjects
                End With


    oAcadSec.Update

    Dim vBoundaryObjs As Variant
    Dim vFillObjs As Variant
    Dim vBackGroundObjs As Variant
    Dim vForeGroundObjs As Variant
    Dim vCurveTangencyObjs As Variant
    
    
    Err.Clear
    setSectionTypeSettings.GenerationOptions = acSectionGenerationDestinationNewBlock
    Debug.Print "1: " & Err.Description
    
    Err.Clear
    vBlockGen = setSectionTypeSettings.DestinationBlock
    Debug.Print "2: " & Err.Description
    
    
    oAcadSec.GenerateSectionGeometry x3DSolid, vBoundaryObjs, vFillObjs, vBackGroundObjs, vForeGroundObjs, vCurveTangencyObjs

    oAcadSec.Delete


End Sub

 

 

 

 

0 Likes
Message 4 of 7

Ed__Jobe
Mentor
Mentor
Accepted solution

I'm really busy right now, but if you post a sample 3D dwg, I'll take a look.

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 5 of 7

davidhellberg
Contributor
Contributor

Hi

 

No worries. Take your time.

Appreciate your help.

I'm surprised there are not more documentation on this matter.

 

Here is a DWG file.

Nothing special about it.

 

Currently I just used a workaround creating the block "manually".

 

/David

0 Likes
Message 6 of 7

Ed__Jobe
Mentor
Mentor
Accepted solution

I edited your code somewhat. Your use of On Error Resume Next was masking the real errors. There are still a few I don't have time to figure out, but it has to do with the section object and its properties. You need to figure out that part to get the results you want. The edited code is below. However, I was able to create this with the VIEWBASE command and right clicking the resulting view and selecting Create Veiw>Section View. It only took a few seconds. So I'm not sure you need to code this. I've attached an updated dwg so you can see the results.

 


Public Sub CreateSectionViewsFrom3DSolid()

    On Error GoTo Err_Control
                          
    Dim oAcadSec As AcadSection
    Dim SectionSettings As AcadSectionSettings
    Dim setSectionTypeSettings As AcadSectionTypeSettings
    Dim dPlaneVector(0 To 2) As Double
    
    Dim x3DSolid As Acad3DSolid
    Dim vBasePt As Variant
    Dim vPt1 As Variant
    Dim vPt2 As Variant

    ' Get the solid
    ' You need to trap for when the user doesn't select a solid.
GetSolid:
    ThisDrawing.Utility.GetEntity x3DSolid, vBasePt, "Pick 3D Solid"
    ' If nothing was selected, the error number will be -2147352567
    ' The error handler catches that. My default is to exit the sub,
    ' but you can change the GoTo to point to GetSolid and it will
    ' prompt the user again until they select a solid or hit {ESC}.
    
    ' get 2 section points
    vPt1 = ThisDrawing.Utility.GetPoint(, "Pick first point")
    ' If you supply vPt1 as the first arg, it will draw a line between the two.
    vPt2 = ThisDrawing.Utility.GetPoint(vPt1, "Pick end point")
    
    
    ' The below vector [0,0,1] will give a vertical section through a solid
    dPlaneVector(0) = 0: dPlaneVector(1) = 0: dPlaneVector(2) = 1


'------------------------------------------------------------------------------
'   Insert Section object
'------------------------------------------------------------------------------
    
    ' Create the section object
    Set oAcadSec = ThisDrawing.ModelSpace.AddSection(vPt1, vPt2, dPlaneVector)
'newly created section is parallel to solid and doesn't intersect with it.
    With oAcadSec
        .Elevation = 350
        .State = acSectionStatePlane
        .State2 = acSectionState2Slice
        .SliceDepth = 250#
        Set SectionSettings = .Settings
        .Update
    End With
    
    With SectionSettings
        .CurrentSectionType = acSectionType2dSection
    End With

    Set setSectionTypeSettings = SectionSettings.GetSectionTypeSettings(acSectionType2dSection)
    
'------------------------------------------------------------------------------
'   Section object settings
'------------------------------------------------------------------------------
    
                ' Section Settings Background lines
                With setSectionTypeSettings
                    .BackgroundLinesVisible = False
                    .BackgroundLinesHiddenLine = False
                    .BackgroundLinesLayer = "PC Bea. Thn" 'Layer doesn't exist
                    .BackgroundLinesLinetype = "ByLayer"
                    
                    '.BackgroundLinesColor=""
                    '.BackgroundLinesLinetypeScale=5
                    '.BackgroundLinesLineweight=0.18
                    '.BackgroundLinesPlotStyleName="UPH Monochrome.ctb"
             
                
                ' Section Settings InterSection boundary
                ' The outline of the Intersection Boundary
'                    .IntersectionBoundaryVisible = True     'This line generates the "Invalid input" error.
                    .IntersectionBoundaryLayer = "PC Pnl. Thn" 'Layer doesn't exist
                    .IntersectionBoundaryLinetype = "ByLayer"
                    
                    '.IntersectionBoundaryColor=""
                    '.IntersectionBoundaryDivisionLines = False
                    '.IntersectionBoundaryLinetypeScale = 5
                    '.IntersectionBoundaryLineweight = 0.18
                    '.IntersectionBoundaryPlotStyleName = "UPH Monochrome.ctb"
                        
                ' Section Settings Intersection fill
                ' For a face when a section goes through the object
                    .IntersectionFillVisible = False
                    '.IntersectionFillColor = "red"
                    '.IntersectionFillFaceTransparency = [long]             [Live Section Only]
                    '.IntersectionFillHatchAngle=45 [units radian??]
                    '.IntersectionFillHatchPatternName = "ANSI31"
                    '.IntersectionFillHatchPatternType = acHatchPatternTypePreDefined
                    '.IntersectionFillHatchScale = 5
                    '.IntersectionFillHatchSpacing = 150#
                    '.IntersectionFillLayer = "PC Htc"
                    '.IntersectionFillLinetype = "Continous"
                    '.IntersectionFillLinetypeScale = 5
                    '.IntersectionFillLineweight = 0.18
                    '.IntersectionFillPlotStyleName = "UPH Monochrome.ctb"
            
                ' Section Settings ForeGround Lines
                ' This is cut away geometry
                    .ForegroundLinesVisible = False
                    .ForegroundLinesHiddenLine = False
                    '.ForegroundLinesColor=""
                    '.ForegroundLinesEdgeTransparency = [long]             [Live Section Only]
                    '.ForegroundLinesFaceTransparency= [long]             [Live Section Only]
                    '.ForegroundLinesLayer = "PC Pnl .Thn"
                    '.ForegroundLinesLinetype = "Continous"
                    '.ForegroundLinesLinetypeScale = 5
                    '.ForegroundLinesLineweight = 0.18
                    '.ForegroundLinesPlotStyleName = "UPH Monochrome.ctb"
            
                ' Section Settings CurveTangency Lines
                    .CurveTangencyLinesVisible = False
                    '.CurveTangencyLinesColor=""
                    '.CurveTangencyLinesLayer="PC Pnl
                    '.CurveTangencyLinesLinetype="Continous"
                    '.CurveTangencyLinesLinetypeScale=5
                    '.CurveTangencyLinesLineweight = 0.18
                    '.CurveTangencyLinesPlotStyleName="UPH Monochrome.ctb"
            
                ' Section Settings Others
                    '.GenerationOptions = acSectionGenerationDestinationNewBlock
                    '.DestinationBlock = "TestBlock"
                    '.DestinationFile = "C:\temp\file.dwg"
                    '.GenerationOptions = acSectionGenerationDestinationFile
                    
                    '.GenerationOptions = acSectionGenerationDestinationReplaceBlock
                    '.GenerationOptions = acSectionGenerationSourceAllObjects
                    '.GenerationOptions = acSectionGenerationSourceSelectedObjects
                    '.SourceObjects = vObjects
                End With


    oAcadSec.Update

    Dim vBoundaryObjs As Variant
    Dim vFillObjs As Variant
    Dim vBackGroundObjs As Variant
    Dim vForeGroundObjs As Variant
    Dim vCurveTangencyObjs As Variant
    
    ' This line now generates the invalid input error
'    setSectionTypeSettings.GenerationOptions = acSectionGenerationDestinationNewBlock
    
    Dim vBlockGen As Variant    ' You didn't declare this var
    vBlockGen = setSectionTypeSettings.DestinationBlock
    
    
    oAcadSec.GenerateSectionGeometry x3DSolid, vBoundaryObjs, vFillObjs, vBackGroundObjs, vForeGroundObjs, vCurveTangencyObjs

    oAcadSec.Delete
    
Exit_Here:
    Exit Sub
Err_Control:
    Select Case Err.Number
    'Add your Case selections here
    Case Is = -2147352567
        ' Nothing was selected.
        Err.Clear
        Resume Exit_Here
    Case Else
        MsgBox Err.Number & ", " & Err.Description, , "RotPoly3D"
        Err.Clear
        Resume Exit_Here
    End Select


End Sub

 

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 7 of 7

davidhellberg
Contributor
Contributor

Thanks for your reply Ed.

 

Appreciate your help.

I will have a look at it.

 

Yeah my overuse of "On Error Resume Next" might jut be because of my lack of programming knowledge.

I learning 🙂

 

/David

0 Likes