DXF Export
The @visuallyjs/browser-ui-export package is an optional add-on for exporting a live Surface or Paper to AutoCAD DXF.
Setup
Install @visuallyjs/browser-ui-export alongside the matching version of @visuallyjs/browser-ui:
npm install @visuallyjs/browser-ui-export
Create an Exporter instance with a Surface or Paper:
How to access the surface
From your app component
To access the surface from inside a component that uses a SurfaceComponent, declare a ref and then use the getSurface() method on it:
import { SurfaceComponent, SurfaceComponentRef } from "@visuallyjs/browser-ui-react"
import "@visuallyjs/browser-ui/css/visuallyjs.css"
import {useRef} from "react"
export default function App() {
const surfaceRef = useRef<SurfaceComponentRef>(null)
const data = { ... }
const renderOptions = {...}
function doSomething() {
const surface = surfaceRef.current.getSurface()
// now you can invoke methods on "surface"
}
return <div style={{width:"100%", height:"500px"}}>
<SurfaceComponent data={data} renderOptions={renderOptions} ref={surfaceRef}/>
</div>
}
From a different component
If you have a component somewhere in your tree from which you wish to interact with the surface, you can use the useSurface() hook in conjunction with a SurfaceProvider to access the surface. Your app component should look something like this:
import { useRef } from "react"
import { SurfaceComponent } from "@visuallyjs/browser-ui-react"
export default function MyComponent() {
return <SurfaceProvider>
<SurfaceComponent ref={surfaceRef}/>
<MyOtherComponent/>
</SurfaceProvider>
}
Then the implementation of MyOtherComponent can use the useSurface hook:
import { useRef } from "react"
import { SurfaceComponent, useSurface } from "@visuallyjs/browser-ui-react"
export default function MyOtherComponent() {
const surfaceRef = useRef<Surface>(null)
useSurface().then(s => surfaceRef.current = s)
function centerContent() {
surfaceRef.current.centerContent()
}
return <div><button onClick={() => centerContent()}>Center content!</button></div>
}
Note the difference between the ref type in the above example from the first example: when you use the useSurface hook the value passed into the promise is a Surface, not a SurfaceComponentRef.
import { Exporter } from "@visuallyjs/browser-ui-export"
const exporter = new Exporter(surface)
AutoCAD DXF Export
exporter.exportDxf(options?) synchronously generates AutoCAD 2007 DXF text from a rendered Surface or Paper. Coordinates preserve the diagram origin, flip the Y axis for CAD space, and ignore viewport pan and zoom.
const { dxf, warnings } = exporter.exportDxf({
units: "mm",
scale: 0.5
})
const file = new Blob([dxf], { type: "application/dxf" })
The exporter writes editable lines, circles, polylines, and single-line text onto four CAD layers: NODES, GROUPS, EDGES, and LABELS. HTML vertices export their measured outlines, port outlines, plain text, and supported inline SVG.
exportDxf
| options | DxfExportOptions |
| Name | Type | Description |
|---|---|---|
| curveTolerance? | number | How closely to fit curves. Defaults to 0.25. |
| renderVertex? | (vertex:ExportVertex) => | undefined | Optional vertex exporter override function. Implement this if you want to provide custom behaviour for the export of your vertices. |
| scale? | number | Scale to use. Defaults to 1. |
| units? | "unitless" | "in" | "ft" | "mm" | "cm" | "m" | Units to use in the DXF. Defaults to "unitless". |
Invalid options throw a RangeError. Any omitted content (such as clipping paths, masks, SVG <use> references, raster images, or rich labels) is reported in warnings (DxfExportWarning[]) with the owning node, group, or edge ID.